Hi,
I am using Hibernate (hibernate-distribution-3.3.0.SP1) and hibernate-annotations-3.4.0.GA with MySQL 5.
I have several entities, e.g. Person, Activity etc. Each of these entities has its own table. 4 columns are common to each table viz. CREATED_TIMESTAMP, LAST_UPDATED_TIMESTAMP, CREATED_BY, and UPDATED_BY. LAST_UPDATED_TIMESTAMP will serve as the versioning column for optimistic locking for all entities. I was trying to model these 4 columns as a component, say, RowLoggingInfo.java that would be present as an attribute of Person.java, Activity.java etc. using @Embedded annotation. However, it seems that it is not possible to have a versioning column in an embedded class, since i get the following error
org.hibernate.AnnotationException: Unable to define @Version on an embedded class: RowLoggingInfo
at org.hibernate.cfg.AnnotationBinder.processElementAnnotations(AnnotationBinder.java:1316)
at org.hibernate.cfg.AnnotationBinder.fillComponent(AnnotationBinder.java:1841)
at org.hibernate.cfg.AnnotationBinder.bindComponent(AnnotationBinder.java:1775)
at org.hibernate.cfg.AnnotationBinder.processElementAnnotations(AnnotationBinder.java:1637)
at org.hibernate.cfg.AnnotationBinder.bindClass(AnnotationBinder.java:754)
at org.hibernate.cfg.AnnotationConfiguration.processArtifactsOfType(AnnotationConfiguration.java:534)
at org.hibernate.cfg.AnnotationConfiguration.secondPassCompile(AnnotationConfiguration.java:286)
at org.hibernate.cfg.Configuration.generateDropSchemaScript(Configuration.java:762)
at org.hibernate.tool.hbm2ddl.SchemaExport.<init>(SchemaExport.java:93)
at org.hibernate.tool.hbm2ddl.SchemaExport.<init>(SchemaExport.java:61)
at CreateDB.createDB(CreateDB.java:69)
This occurred while trying to create the DB tables using:
public void createDB() {
AnnotationConfiguration cfg = new AnnotationConfiguration().configure();
SchemaExport schemaExport = new SchemaExport(cfg);
schemaExport.create(true, true);
}
I would prefer to be able to use this approach, rather than having these 4 properties as attributes in each of the persistent classes. Neither do i prefer using @MappedSuperClass since there is logically no inheritence hierarchy between this logging information and entities like Person and Activity. Any pointers on how to achieve this are highly appreciated.
Following are snippets of the concerned code:
@Embeddable
public class RowLoggingInfo {
private Date createdTime;
private Date lastUpdatedTime;
// CREATED_BY, UPDATED_BY properties left out for brevity
@org.hibernate.annotations.Generated(org.hibernate.annotations.GenerationTime.ALWAYS)
@Column(name = "CREATED_TIMESTAMP", nullable = false, columnDefinition="timestamp default CURRENT_TIMESTAMP")
public Date getCreatedTime() {
return createdTime;
}
public void setCreatedTime(Date someCreatedTime) {
createdTime = someCreatedTime;
}
@Version
@Column(name = "LAST_UPDATED_TIMESTAMP", nullable = false)
public Date getLastUpdatedTime() {
return lastUpdatedTime;
}
public void setLastUpdatedTime(Date someLastUpdatedTime) {
lastUpdatedTime = someLastUpdatedTime;
}
}
Snippet of Person.java :
@Entity
public class Person
{
.....
private RowLoggingInfo logInfo;
@Embedded
public RowLoggingInfo getLogInfo() {
return logInfo;
}
public void setLogInfo(RowLoggingInfo someLogInfo) {
logInfo = someLogInfo;
}
.....
}
TIA,
Best Regards,
-abhi
|