@Version 不会在关系更改时增加
根据JPA 2.0规范:
版本属性由持久化提供程序运行时更新 当对象写入数据库时。一切无关系 字段和属性以及实体拥有的所有关系 包含在版本检查中。
在我的情况下,这似乎不起作用。我有两个实体:
@Entity
public class OrderedItem {
@Id
@GeneratedValue
private int id;
private String name;
@ManyToOne
private Customer owner;
@Version
private int version;
public OrderedItem(String name) {
this.name = name;
}
// default constructor + bunch of simple accessors and mutators
}
@Entity
public class Customer {
@Id
@GeneratedValue
private int id;
private String name;
@OneToMany(mappedBy = "owner")
private Set<OrderedItem> orderedItems = new HashSet<OrderedItem>();;
@Version
private int version;
public Customer(String name) {
this.name = name;
}
// default constructor + bunch of simple accessors and mutators
}
ManyToOne
关系的 “many” 方必须是拥有方,因此在我的情况下,OrderedItem
是拥有方。遵循 JPA 2.0 规范,我假设当我更改从 OrderedItem
访问的 Customer
对象时,OrderedItem
实体的版本应该增加。因此,我尝试测试它:
Customer john = new Customer("John");
OrderedItem milk = new OrderedItem("Milk");
milk.setOwner(john);
john.getOrderedItems().add(milk);
startTx();
em.persist(john);
em.persist(milk);
stopTx();
startTx();
OrderedItem milkFromPC = em.find(OrderedItem.class, milk.getId());
milkFromPC.getOwner().setName("Michael");
stopTx();
OrderedItem
的版本号为 1(因此没有增量),Customer 的版本号为 2
。
有什么想法吗?
在写这个问题时,我突然想到 - JPA 2.0 规范中的“关系”一词是否意味着仅如果我更改实体 - 客户
- 本身(不是其状态)版本会增加吗?就像 @JoinColumn(updatable=false)
的情况一样?
According to the JPA 2.0 specification:
The version attribute is updated by the persistence provider runtime
when the object is written to the database. All non-relationship
fields and properties and all relationships owned by the entity are
included in version checks.
This doesn't seem to work in my case. I have two Entities:
@Entity
public class OrderedItem {
@Id
@GeneratedValue
private int id;
private String name;
@ManyToOne
private Customer owner;
@Version
private int version;
public OrderedItem(String name) {
this.name = name;
}
// default constructor + bunch of simple accessors and mutators
}
@Entity
public class Customer {
@Id
@GeneratedValue
private int id;
private String name;
@OneToMany(mappedBy = "owner")
private Set<OrderedItem> orderedItems = new HashSet<OrderedItem>();;
@Version
private int version;
public Customer(String name) {
this.name = name;
}
// default constructor + bunch of simple accessors and mutators
}
The "many" side of ManyToOne
relationship must be an owning side, so in my case the OrderedItem
is the owning side. Following the JPA 2.0 spec, I assume that when I change the Customer
object accessed from OrderedItem
the version of OrderedItem
entity should be incremented. Therefore, I've tried to test it:
Customer john = new Customer("John");
OrderedItem milk = new OrderedItem("Milk");
milk.setOwner(john);
john.getOrderedItems().add(milk);
startTx();
em.persist(john);
em.persist(milk);
stopTx();
startTx();
OrderedItem milkFromPC = em.find(OrderedItem.class, milk.getId());
milkFromPC.getOwner().setName("Michael");
stopTx();
The version numbers are 1 for OrderedItem
(so no increment) and 2 for Customer
.
Any ideas why?
It just bumped into my head while writing this question - is it possible that the "relationship" word in JPA 2.0 specification means that only if I change the entity -- Customer
-- itself (not its state) the version will be incremented? Just like in case of @JoinColumn(updatable=false)
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
是的,据我了解,如果您重新分配不同的客户,则 OrderedItem 的版本字段将会更改,但如果您更改客户的属性则不会更改。
另一方面,如果您将另一个 OrderedItem 添加到 Customer 的 OrderedItem 集中,则 Customer 的版本字段不会更新,因为它不是拥有方。
我想在这里在表行中思考是有用的:如果表行中的任何列发生更改,则该表行的版本列会增加。
如果一个客户端更新 OrderedItem 的名称,而另一个客户端更新 Customer 的名称,这很好 - 这些更新不会发生冲突。
Yes, as I understand it the version field of OrderedItem will change, if you reassign a different Customer, but not if you change a property of the customer.
On the other hand, if you add another OrderedItem to the OrderedItem set of the Customer, the version field of the Customer doesn't get updated because it's not the owning side.
I guess it's useful here to think here in table rows: if any column in a table row changes the version column of that table row gets incremented.
It's fine if one client updates the name of the OrderedItem and another the name of the Customer - these updates don't conflict.