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

时间:2012-11-19 10:20:23

标签: java hibernate jpa locking jpa-2.0

我有以下实体结构:


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....

}

具体实体:



@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 ....

}

现在,我从数据库中选择MyEntity类型的实体,用entityManager.lock(myEntityInstance, LockModeType.OPTIMISTIC);锁定它,修改它,并保持修改。我期望获得的是让oplock列的值递增(oplock是版本列),但它不受影响。

问题:行为正确还是遗漏了什么?我认为正确的行为是oplock的值会增加。


编辑:我从hibernate 3.6切换到hibernate 4.1后,我仍然会遇到相同的行为。

1 个答案:

答案 0 :(得分:1)

最后我解决了我的问题:

我只在@MappedSuperclass课程上面添加BaseEntity注释。有关详细信息,请参阅here