简体   繁体   中英

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); , 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.

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.


Edit: After I switched from hibernate 3.6 to hibernate 4.1, I still get the same behavior.

Finally I solved my problem:

I put only @MappedSuperclass annotation above BaseEntity class. For more details you can read here .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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