-->
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.  [ 5 posts ] 
Author Message
 Post subject: Custom Id generation
PostPosted: Thu Mar 29, 2007 6:14 am 
Senior
Senior

Joined: Mon Jul 24, 2006 8:43 am
Posts: 160
hi,
I am looking for help writing and using a custom Id Generator to generate compound primary keys.
I have checked the javadoc:(http://www.hibernate.org/hib_docs/annotations/api/) for org.hibernate.mapping.IdGenerator but it's not obvious how one would go about using this. Also I can't find a good link from Google or in the Hibernate book I am reading (Gavin King, Manning).

Does anybody have a good link giving an example of writing a custom Id Generator for generating compound primary keys.

Kind Regards


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 29, 2007 7:59 am 
Expert
Expert

Joined: Tue Dec 28, 2004 7:02 am
Posts: 573
Location: Toulouse, France
It's not possible to write id generator for composite keys.
See http://www.hibernate.org/hib_docs/v3/re ... ompositeid
Quote:
You can't use an IdentifierGenerator to generate composite keys. Instead the application must assign its own identifiers.


In fact, it doesn't make sense to have generated composite keys: composite keys should be business manually assigned keys. Else when generated, it would be a lot more simple to define a simple column for the generated unique identifier.

_________________
Baptiste
PS : please don't forget to give credits below if you found this answer useful :)


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 29, 2007 8:05 am 
Senior
Senior

Joined: Mon Jul 24, 2006 8:43 am
Posts: 160
batmat wrote:
It's not possible to write id generator for composite keys.
See http://www.hibernate.org/hib_docs/v3/re ... ompositeid
Quote:
You can't use an IdentifierGenerator to generate composite keys. Instead the application must assign its own identifiers.


In fact, it doesn't make sense to have generated composite keys: composite keys should be business manually assigned keys. Else when generated, it would be a lot more simple to define a simple column for the generated unique identifier.

Fair point. But I am trying to integrate with legacy database, a requirement is to still use composite keys and to to only assign them before the commit. The only way to do is to make the application code do it every time, this is cumbersome and bloats out the application code.

Comments appreciated.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 29, 2007 9:33 am 
Expert
Expert

Joined: Tue Dec 28, 2004 7:02 am
Posts: 573
Location: Toulouse, France
As it's not possible to define generator for composite ids, maybe one solution could to define an Interceptor for this: you would intercept beforeSave() if (yourInstance instanceof YourClassWhichHasCompositeId) or something like this and call your special processing before calling the real save.

But please note I've no idea if this method would have some potential risk. Maybe it'd better than nothing?

_________________
Baptiste
PS : please don't forget to give credits below if you found this answer useful :)


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 30, 2007 7:13 am 
Senior
Senior

Joined: Mon Jul 24, 2006 8:43 am
Posts: 160
I have tried doing something like what you say using the interceptor
method onSave(), the problem is that before the Hibernate framework fires the onSave event it checks to see if the entity has a primary key, if not it will throw an exception.

The same happens if you use the prePersist method.

One way around this would be to assign the Entity a POJO key a default key and then check for this, however at this stage things are getting a bit messy, and I am wondering there must be a cleaner approach.


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

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.