Code Junkie
Code Junkie

Reputation: 7788

Hibernate timestamp version controlled by database.

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?

Upvotes: 3

Views: 6922

Answers (1)

Markus
Markus

Reputation: 2053

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;

Upvotes: 3

Related Questions