Envers @OneToMany audit on CREATE(0) but not on DE

2019-08-02 19:33发布

问题:

I've looked a quite a few similar issues like: http://community.jboss.org/message/580407#580407 but haven't found a solution yet.

An Activity has many Occurences, when an occurence is created the activity_occurence_AUD table is updated correctly with a 0 (create) revision.

However when an occurance is removed the activity_occurence_AUD table is not populated with a 2 (delete) revision.

Activity Entity:

@Entity
@Table(name = "activity")
@Audited
public class Activity implements Serializable {
    private static final long serialVersionUID = 1L;

    public static final int[] VALID_PRIORITIES = { 0, 1, 2, 3 };

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Basic(optional = false)
    @Column(name = "id", nullable = false)
    private Long id;

    @OneToMany(cascade = CascadeType.ALL, mappedBy = "activity")
    private List<ActivityOccurrence> activityOccurrenceList;

....

}

ActivityOccurence Entity:

@Entity
@Table(name = "activity_occurrence")
@Audited    
public class ActivityOccurrence implements Comparable<ActivityOccurrence>, Serializable {
    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Basic(optional = false)
    @Column(name = "id", nullable = false)
    private Long id;

    @JoinColumn(name = "activity_id", referencedColumnName = "id", nullable = false)
    @ManyToOne(optional = false)
    private Activity activity;

....

}

hibernate properties:

<entry key="hibernate.ejb.event.post-insert"     
  value="org.hibernate.ejb.event.EJB3PostInsertEventListener,org.hibernate.envers.event.AuditEventListener" />
<entry key="hibernate.ejb.event.post-update"
  value="org.hibernate.ejb.event.EJB3PostUpdateEventListener,org.hibernate.envers.event.AuditEventListener" />
<entry key="hibernate.ejb.event.post-delete"
  value="org.hibernate.ejb.event.EJB3PostDeleteEventListener,org.hibernate.envers.event.AuditEventListener" />
<entry key="hibernate.ejb.event.pre-collection-update"
  value="org.hibernate.envers.event.AuditEventListener" />
<entry key="hibernate.ejb.event.pre-collection-remove"
  value="org.hibernate.envers.event.AuditEventListener" />
<entry key="hibernate.ejb.event.post-collection-recreate" 
  value="org.hibernate.envers.event.AuditEventListener" />

Any help would be much appreciated.

It's strange that the update works but the delete doesn't.

Let me know if I can provide any more information.

回答1:

Digging up an old one here but think I know the answer, but only because I have run into the same problem this weekend.

Are you doing:

Activity.getActivityOccurrenceList().remove(OCCURRENCE);

Or are you doing:

Activity.setActivityOccurrenceList(NEW_LIST_EXLUDING_REMOVED_OCCURRENCE);

The first should give you a REVTYPE of 2, whereas the second option will probably give you a REVTYPE of 0.

Of course I could be wrong, because my example is ManyToMany and has a join table but from my tinkering this is how I think it's working.

In my case, but using your example; spring is biding the list of activity occurrences, and spring creates a new list each time to do this, resulting in the REVTYPE of 0 (ADD) even though I actually removed an occurrence.

Did you eventually find a solution to this yourself? If so could you share?