简体   繁体   English

使用JPA(Hibernate)不会增加版本列

[英]version column does not get increased with JPA (Hibernate)

I have the following Entity structure: 我有以下实体结构:


public abstract class BaseEntity {

    private int _version;

    public BaseEntity() {
    }

    @Version
    @Column(name = "oplock", nullable = false)
    private int getVersion() {
        return _version;
    }

    @SuppressWarnings("unused")
    private void setVersion(final int version) {
        _version = version;
    }
    //some other code goes here....

}

the concrete Entity: 具体实体:



@Table(name="my_table", schema="public", 
    uniqueConstraints=@UniqueConstraint(columnNames={"username", "attribute"})
)
public class MyEntity extends BaseEntity implements java.io.Serializable {

    private int id;
    private String username;
    private String attribute;
    private String op;
    private String value;

    public MyEntity() {
    }
    //some code goes here ....

}

Now, I select from the database an entity of MyEntity type, lock it with entityManager.lock(myEntityInstance, LockModeType.OPTIMISTIC); 现在,我从数据库中选择一个MyEntity类型的实体,用entityManager.lock(myEntityInstance, LockModeType.OPTIMISTIC);锁定它entityManager.lock(myEntityInstance, LockModeType.OPTIMISTIC); , modify it, and persist modifications. ,修改它,并坚持修改。 The thing that I expect to get is to have oplock column's value incremented ( oplock is the version column), but it is untouched. 我期望获得的是使oplock列的值递增( oplock是版本列),但它不受影响。

Question: Does it behave right or am I missing something? 问题:它表现得正确还是我错过了什么? I think the right behavior would be that the oplock 's value would be incremented. 我认为正确的行为是oplock的值会增加。


Edit: After I switched from hibernate 3.6 to hibernate 4.1, I still get the same behavior. 编辑:从hibernate 3.6切换到hibernate 4.1后,我仍然得到相同的行为。

Finally I solved my problem: 最后我解决了我的问题:

I put only @MappedSuperclass annotation above BaseEntity class. 我只将@MappedSuperclass注释放在BaseEntity类之上。 For more details you can read here . 有关详细信息,请阅读此处

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM