JPA 乐观锁 - 将 @Version 设置为实体类会导致查询包含 VERSION 作为列

发布于 2024-10-09 17:33:28 字数 825 浏览 0 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

浪荡不羁 2024-10-16 17:33:28

您应该将版本作为数字列添加到表中。该列将用于乐观锁定。

但我确实更喜欢乐观锁定的日期字段,这样您就可以跟踪该对象何时发生更改。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文