开发者

JPA optimistic lock - setting @Version to entity class cause query to include VERSION as column

开发者 https://www.devze.com 2023-02-01 23:39 出处:网络
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 q

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 St开发者_运维技巧ring 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 


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消