如果修改了父对象的一对多关系,则父对象的版本不会增加 [英] Version of parent object is not incremented if one-to-many relationship of the parent object is modified

查看:78
本文介绍了如果修改了父对象的一对多关系,则父对象的版本不会增加的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将EclipseLink用作JPA提供程序。我面临的问题是,每当在父对象中添加/删除孩子时,版本属性就不会更新。

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.

有一家公司。 Company类具有属性Set。公司将版本字段分类。每当我更新任何非关系属性时,公司实例的版本字段都是增量。但是,如果我将Employee类的实例添加到一对多集合雇员,则Company实例的version字段不会增加。基本上,如果将子级添加到父级对象或从父级对象中删除,则父级的版本不会增加。

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会增加父级对象的版本。

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

EclipseLink的行为方式是否相同?我要丢失什么吗?

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

预先感谢。

推荐答案

每当任何直接字段或自有关系发生更改时,缺省EclipseLink都会递增锁定。我相信这就是JPA规范所规定的。使用 mappedBy的OneToMany不被视为拥有关系,因此默认情况下不会导致版本增加。

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)

您还可以使用DescriptorCustomizer进行设置(导致对关系的任何更改以增加版本)

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

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

这篇关于如果修改了父对象的一对多关系,则父对象的版本不会增加的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆