Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Help Needed....

 
shri mon
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
shri mon
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot....
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic