Hibernate Envers:-Addition of Object in Collection member variable also creates a entry in Audit Table of Main Class

StackOverflow https://stackoverflow.com//questions/25057367

  •  21-12-2019
  •  | 
  •  

Question

There are 2 classes which are both audited.

@Entity
@Audited
@Table(name = "PLACE")
public class Place {
private long woeId;
private Set<PlaceName> placeNames;

@OneToMany(mappedBy = "place", fetch = FetchType.EAGER, cascade = CascadeType.ALL, orphanRemoval = true)
@Fetch(FetchMode.JOIN)
public Set<PlaceName> getPlaceNames() {
    return placeNames;
}

....
}


@Entity
@Audited
@Table(name = "PLACE_NAME")
@Check(constraints = "NAME_TYPE in ('N', 'V', 'A', 'S', 'P', 'Q', 'A1', 'A2', 'A3', 'FA', 'FI', 'IA', 'IC', 'I2', 'I3', 'NA', 'P1', 'P2')")
@Cache(usage=CacheConcurrencyStrategy.READ_WRITE)
public class PlaceName implements Serializable {
    private long id;
    private String name;
    private Place place;

    @Id
    @SequenceGenerator(name = "SEQ_STORE", sequenceName = "PLACE_NAME_SEQ", allocationSize = 1)
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "SEQ_STORE")
    @Column(name = "NAME_ID", columnDefinition = "NUMBER(12, 0)", nullable = false)
    public long getId() {
        return id;
    }

    /**
     * @param id
     *            the id to set
     */
    public void setId(long id) {
        this.id = id;
    }

    @Column(name = "NAME", columnDefinition = "NVARCHAR2(512)", nullable = false)
    public String getName() {
        return name;
    }

    /**
     * @param name
     *            the name of the {@link Place}
     */
    public void setName(String name) {
        this.name = name;
    }

    /**
     * @return the {@link Place} this name is associated with
     */
    @ManyToOne
    @JoinColumn(name = "WOE_ID")
    public Place getPlace() {
        return place;
    }

    /**
     * @param place
     *            the {@link Place} this name is associated with
     */
    public void setPlace(Place place) {
        this.place = place;
    }

}

So, when a place name is added in PLACE_NAME table using

Place place = session.get(Place.class, new Integer(1));
PlaceName pn = new PlaceName();
pn.setName("ABC");
pn.setPlace(place);

then, in PLACE_NAME_AUD table, a entry is inserted but also a entry is added in PLACE_AUD table? What is the reason of that?

Was it helpful?

Solution

That's because looking at Place, the collection changed. This is configurable via the org.hibernate.envers.revision_on_collection_change config property. See the docs for details.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top