wood burning stoves 2.0*
The moose likes Object Relational Mapping and the fly likes Help Needed.... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Help Needed...." Watch "Help Needed...." New topic
Author

Help Needed....

shri mon
Ranch Hand

Joined: Jan 11, 2005
Posts: 63
I have a table where a combination of two columns is a primary key.
How should i specify that in the mapping file?

Thanks in advance.

Shrimon.
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

Ideally, if you can change the data model, give this table a surrogate key. Its far easier in the long run.

If you can't, you need to map the composite key. To do this Hibernate defines a composite-id element you use instead of the normal id element in your mapping files. You could map the key like this:

In order for Hibernate to properly manage object identity with a composite key, you need to override the equals() and hashCode() methods in your POJO class. This way your POJO itself is its own key.

The alternative, and more elegant, way is to define a seperate identifier class which contains the two properties in your key and overrides equals() and hashCode(). If you did this, your mapping looks like this:

[ April 22, 2005: Message edited by: Paul Sturrock ]

JavaRanch FAQ HowToAskQuestionsOnJavaRanch
shri mon
Ranch Hand

Joined: Jan 11, 2005
Posts: 63
Thanks a lot....
 
jQuery in Action, 2nd edition
 
subject: Help Needed....
 
Similar Threads
Cant use object from another class
WA #1.....word association
Posting to bump up my count...
Text box value is not being completely filled with request attribute
The x900 effect ?