@OneToMany make revision at insert but not at update
mehdizehtaban May 14, 2011 7:07 AMI searched the forum but couldn't find out my similar problem. Here is it :
I have an entity Called Person that is the parent of Customer Entity:
@Entity
@Table(name = "my_person")
@TableGenerator(name="SEQ_GEN", table = "tbl_seq" , pkColumnName = "seq_name" , pkColumnValue = "my_person", valueColumnName = "seq_value", allocationSize = 50)
@Audited
public class MyPerson {
@Id
@Column(name = "person_id")
@GeneratedValue(generator = "SEQ_GEN")
public Long getPersonId() {
return personId;
}
private void setPersonId(Long personId) {
this.personId = personId;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
@OneToMany(fetch = FetchType.LAZY, targetEntity = MyCustomer.class, mappedBy = "person"
, cascade = {CascadeType.ALL})
public List<MyCustomer> getCustomers() {
return customers;
}
public void setCustomers(List<MyCustomer> customers) {
this.customers = customers;
}
@Version
public Integer getVersion() {
return version;
}
public void setVersion(Integer version) {
this.version = version;
}
Long personId;
String firstName;
String lastName;
List<MyCustomer> customers;
Integer version;
}
And Customer Class :
package test.envers;
import com.foursun.cymap.common.data.to.PersonTO;
import com.foursun.util.spring.SpringUtils;
import org.hibernate.envers.Audited;
import org.hibernate.envers.ModificationStore;
import org.hibernate.envers.RelationTargetAuditMode;
import javax.persistence.*;
/**
* @author Mehdi Zehtaban
* Date: May 14, 2011
* Time: 1:11:25 PM
*/
@Entity
@Table(name = "my_customer")
@TableGenerator(name="SEQ_GEN", table = "tbl_seq" , pkColumnName = "seq_name" , pkColumnValue = "my_customer", valueColumnName = "seq_value", allocationSize = 50)
@Audited
public class MyCustomer {
@Id
@Column(name = "customer_id")
@GeneratedValue(generator = "SEQ_GEN")
public Long getCustomerId() {
return customerId;
}
private void setCustomerId(Long customerId) {
this.customerId = customerId;
}
public String getPanel() {
return panel;
}
public void setPanel(String panel) {
this.panel = panel;
}
@Audited
@ManyToOne(fetch = FetchType.LAZY, targetEntity = MyPerson.class)
@JoinColumn(name = "person_id")
public MyPerson getPerson() {
return person;
}
public void setPerson(MyPerson person) {
this.person = person;
}
@Transient
public Long getPersonId() {
if (this.person != null){
return this.person.getPersonId();
}
return null;
}
public void setPersonId(Long personId) {
if (personId != null){
this.person = SpringUtils.getEntityManager().getReference(MyPerson.class, personId);
}
}
@Version
public Integer getVersion() {
return version;
}
public void setVersion(Integer version) {
this.version = version;
}
Long customerId;
String panel;
MyPerson person;
Integer version;
}
when I create a new instance of Customer a new revision of Person will be inserted in person_aud.
but when I update a customer envers dont make a revision for Person.
In some cases I need to make a new revision for parent relation whenever the child relation changed.
And in some cases I dont need to.
Is there any way to configure the relations?