-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: Use attribute of an embedded object (Component) for Version
PostPosted: Wed Sep 24, 2008 6:27 am 
Newbie

Joined: Wed Sep 24, 2008 6:09 am
Posts: 1
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


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.