JPA:设置 @JoinColumn(updatable = false) 以避免 OptimisticLockException
给定以下两个实体
@Entity
public class A {
@Version
protected int version;
String basicPropertey;
// getter and setter for basicProperty
}
@Entity
public class B {
@Version
protected int version;
@ManyToOne
private A a;
public B(A a) {
this.a = a}
}
//getter for a
}
两个问题:
实体 A 的版本号是否每次都会增加 实体 B 被合并到 DB(注意,没有 CascadeType.MERGE 定义在与 A) 的关系上,因此,导致 当 A 与 a 合并时可能出现 OptimisticcLockException 不同的版本号?
如果是,是否有助于避免 如果我添加
@JoinColumn(updatable=false)
在 关系?
我的情况是实体 B 更新非常频繁,而 A 则偶尔更新,但只有其基本属性(而不是关系),并且我在实体 A 上收到 OptimisticLockException。
Given the following the following two entities
@Entity
public class A {
@Version
protected int version;
String basicPropertey;
// getter and setter for basicProperty
}
@Entity
public class B {
@Version
protected int version;
@ManyToOne
private A a;
public B(A a) {
this.a = a}
}
//getter for a
}
Two questions:
Is there version number of the entity A increased every time the
entity B is merged to the DB (note that there is no
CascadeType.MERGE defined on the relationship to A) and, thus, cause
a possible OptimisticcLockException when A is merged with a
different version number?If yes, would it help to avoid the likelihood of an
OptimisticLockException if I add@JoinColumn(updatable=false)
on the
relationship?
My situation is that entity B is updated very frequently and A sporadically, but only its basic attributes (not the relationship) and I'm getting an OptimisticLockException on the entity A.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您只更改 B,那么您不会在 A 上收到锁定错误。
您只会在 B 上收到锁定错误
。检查 SQL 日志以了解您实际执行的操作。
If you only change B, then you cannot get a lock error on A.
You will only get a lock error on B.
Check you SQL log to what you are actually doing.