我正在尝试openjpa和jpa。我只有一个实体类作为数据库中的对应表。实体的一个属性是用户名,db表中的相应行有varchar2(20)。在我的主要方法中,我试图坚持使用用户名超过20的实体的实例。 我所做的只是
em.getTransaction().begin();
em.persist(entity); //entity here is the instance with the username longer than 20
em.getTransaction().commit();
我试过这个,希望得到一些其他类型的异常,但我不知道为什么我得到optimisticklockexception。
我没有任何锁定设置。我的意思是我使用默认值来锁定属性。
有人知道这里发生了什么吗?
答案 0 :(得分:0)
不确定为什么会发生这种情况......我注意到在奇怪的情况下可能会抛出OptimisticLockException ......
向您的表和实体添加版本字段通常可以使OpenJPA更好地锁定...
在你的实体bean中添加它(也将名为VERSION的列添加到你的表中):
private Long version;
@Version
@Column(name="VERSION")
public Long getVersion() {
return version;
}
public void setVersion(Long version) {
this.version = version;
}
希望这会有所帮助......