Hibernate timestamp version controlled by database.

假如想象 提交于 2019-12-07 14:52:49

问题


I'm using both Hibernate annotations and Sybase. I'm looking to setup a version column to prevent locking. The database needs to administrate the timestamp rather than the application. I would like to accomplish this using annotations rather than hbm.xml.

I've tried the following without success,

I read on jboss.org to use

@org.hibernate.annotations.SourceType.DB
@org.hibernate.annotations.Generated(GenerationTime.ALWAYS)

however I'm getting an IDE compiling error for DB, "cannot find symbol symbol: class DB location: class SourceType"

and a compiling error for rowVersion,

The Version field or property is not one of the supported types. Make sure that it is one of the following types: int, Integer, short, Short, long, Long, java.sql.Timestamp.

A temporal attribute must be marked with the @Temporal annotation.

http://docs.jboss.org/hibernate/core/3.6/reference/en-US/html/mapping.html#d0e5785

5.1.3.2. Timestamp

Sample Code

@Version
@org.hibernate.annotations.SourceType.DB
@org.hibernate.annotations.Generated(GenerationTime.ALWAYS)
@Column(name = "row_version")
private Date rowVersion;

public Date getRowVersion() {
    return rowVersion;
}

public void setRowVersion(Date rowVersion) {
    this.rowVersion = rowVersion;
}

Could someone tell me what I'm missing?


回答1:


This is not an annotation, but a field of an enum:

@org.hibernate.annotations.SourceType.DB

You need this on your field:

@Version
@org.hibernate.annotations.Source(SourceType.DB)
@org.hibernate.annotations.Generated(GenerationTime.ALWAYS)
@Column(name = "row_version") //maybe unnecessary, because this annotation
                              //is only needed, if the column name does not
                              //match hibernate's default naming strategy
                              //(or custom strategy).
@Temporal(TemporalType.TIMESTAMP)
private Date rowVersion;


来源:https://stackoverflow.com/questions/12864826/hibernate-timestamp-version-controlled-by-database

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!