Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Composite Unique Key problem in Hibernate.

 
Zee Ho
Ranch Hand
Posts: 128
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
All ranchers:
I have got the problem in mapping 2 properties to a composite unique key in hibernate. The target sql script should be like this:

acl_object_identity, recipient are the composite unique key. what I have done is introduce a new class which contains acl_object_identity, recipient,and put the new class as composite-element in my mapping file.


that works. but make my domain object very ugly.

Anyone can give me a hint about how to do it in a nice way. I am quite new to hibernate.
 
pascal betz
Ranch Hand
Posts: 547
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
there is no way around having a class representing your composite key. You need proper equals/hashCode implementation for your key object.

I don't see what's ugly about this... but i would suggest you drop the composite key and use a surrogate key. easier to handle.


pascal
 
Zee Ho
Ranch Hand
Posts: 128
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But the new class introduced is only for the composite key, I think the hibernate is designed for resolving the inconsistence of Data Model and Object Model. Now, I changed my class just to conform the data model. that does not look nice from my point of view. What you said is good suggestion, but not for my case, acutally MASK is the number in certain arrange, becasue it identify the permission acutally.
 
pascal betz
Ranch Hand
Posts: 547
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes, it is only for the composite key. Hibernate needs a Serializable key Object to identify the instatiated entity (e.g. in 1st/2nd level cache, for relationships, in collections) so there is no way around this (all the other keys: String, Long, Integer, ... have a proper hashCode/equals method implementation and are Serializable too). Think of the Composite key class to be on the same level as these classes :-)

pascal
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Of course you don't need a seperate class to represent your composite key, you can just use the POJO itself. OK you still need to implement equals/hashCode so equality becomes the key plus the type, but you don;t need the MyEntityCompositKey class anymore.
 
pascal betz
Ranch Hand
Posts: 547
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
but if the POJO itself is the key class and you need to override the equals/hashCode methods, these methods will compare on the key properties (DB identity). Usualy these properties are only set when the entity gets persisted and this will likely cause troubles in collections (e.g. Set). And if you have some sort of "business equality" (e.g. a user is equals to another user if he has the same username) then .....


pascal
 
Zee Ho
Ranch Hand
Posts: 128
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks guys
 
Umesh Kamat
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I know this is old post, but might somebody looking for this feature. I think you require <natural-id> of hibernate mapping. It is used for group the multiple columns for uniqueness.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic