JPA 乐观锁 - 将 @Version 设置为实体类会导致查询包含 VERSION 作为列
我正在使用 JPA Toplink Essential、Netbean6.8、GlassFish v3
在我的实体类中,我添加了 @Version 注释以在事务提交时启用乐观锁定,但是在添加注释之后,我的查询开始包含 VERSION 作为列,从而引发 SQL 异常。
到目前为止,我见过的任何教程都没有提到这些。可能出什么问题了?
使用的代码段
public class MasatosanTest2 implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@Basic(optional = false)
@Column(name = "id")
private Integer id;
@Column(name = "username")
private String username;
@Column(name = "note")
private String note;
//here adding Version
@Version
int version;
查询:
SELECT m FROM MasatosanTest2 m
Internal Exception: com.microsoft.sqlserver.jdbc.SQLServerException
Call: SELECT id, username, note, VERSION FROM MasatosanTest2
I'm using JPA Toplink Essential, Netbean6.8, GlassFish v3
In my Entity class I added @Version annotation
to enable optimistic locking
at transaction commit however after I added the annotation, my query started including VERSION as column thus throwing SQL exception.
None of this is mentioned in any tutorial I've seen so far. What could be wrong?
Snippet
public class MasatosanTest2 implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@Basic(optional = false)
@Column(name = "id")
private Integer id;
@Column(name = "username")
private String username;
@Column(name = "note")
private String note;
//here adding Version
@Version
int version;
query used:
SELECT m FROM MasatosanTest2 m
Internal Exception: com.microsoft.sqlserver.jdbc.SQLServerException
Call: SELECT id, username, note, VERSION FROM MasatosanTest2
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您应该将版本作为数字列添加到表中。该列将用于乐观锁定。
但我确实更喜欢乐观锁定的日期字段,这样您就可以跟踪该对象何时发生更改。
You should add version as numeric column to your table. This column will be used for optimistic locking.
But I do prefer date field for optimistic locking, in that way you can track when that object has changed.