Version of parent object is not incremented if one-to-many relationship of the parent object is modified

南楼画角 提交于 2019-12-11 10:45:52

问题


I am using EclipseLink as a JPA provider. I am facing issue with version attribute not being updated whenever a child is added/removed in parent object.

There is a class Company. The Company class has a property Set. The Company class a version field. Whenever I update any non-relationship attribute, the version field of Company instance is increment. However, if I add an instance of Employee class to one-to-many collection employees, version field of Company instance is NOT incremented. Basically, if a child is added to or removed from parent object, version of parent is not incremented.

@Entity
class Company {
   @Id
   private long id;
   private String name;
   @Version
   private Integer version;
   @OneToMany(mappyedBy="company", cascade = CascadeType.ALL, orphanRemoval = true)
   private Set<Employee> employees;
   //
   // setter / getter methods
}

@Entity
class Employee {
   @Id
   private long id;
   private String name;
   @ManyToOne
   @JoinColumn(name = "compid")
   private Company company; 
   //
   // setter / getter methods
}

Hibernate increments version of a parent object if a child is added to or removed from the parent object.

Should not EclipseLink behave the same way? Am I missing something?

Thanks in advance.


回答1:


By default EclipseLink increments the lock whenever any direct field, or "owned" relationship changes. I believe this is what the JPA spec dictates. A OneToMany that uses a "mappedBy" is not considered an owned relationship, so does not cause the increment of the version by default.

You can configure this using, (causes any change to any relationship or dependent object to increment version)

@OptimisticLocking(cascade=true)

You can also use a DescriptorCustomizer to set, (causes any change to any relationship to increment version)

descriptor.getOptimisticLockingPolicy().setLockOnChangeMode(LockOnChange.ALL)


来源:https://stackoverflow.com/questions/13721354/version-of-parent-object-is-not-incremented-if-one-to-many-relationship-of-the-p

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!