• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Object Relational Mapping Help

 
rahul karwal
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I have a table with 3 columns "username" "property" and "value" and all 3 combine to form a primary key for each record? what would be the ORM mapping for this ? pls help.

Eg

username property value
test email aa@aaa.com
test email bb@bbb.com

thanks
aziz
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Which ORM product are you using?
 
rahul karwal
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
i am using Hibernate 3.0
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK you will need to map this as a composite key (for that is what it is). Composite keys and Hibernate don't go too well together; the Hibernate developers take the view that composite keys are unnecessary in real data models (and I have to say I agree with them 99% of the time).

To do this you will need to replace the normal <id /> element with a <composite-id> element. Basically:

Then you have to choose what strategy you will use to identify the object. Normally, with a single value id, Hibernate just uses the equals() method for what ever object type the id is. But with a composite-id, the type of the id is the object itself. So your POJO will have to override equals() and hashCode() to reflect this. Alternatively you can create a distinct composite key class to use, for example:

You would create the IdClass, which would be a serializable class which contained the properties of your key and implemented equals() & hashCode().

If this is all too horrible and you can change your table definition to use a surrogate key.
 
rahul karwal
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
since the table contains only 3 columns and all of them combine to form primary key and if i am using
<composite-id>
<key-property name="test" column="test" />
<key-property name="property" column="property" />
<key-property name="value" column="value" />
</composite-id>

then i dont need to mention
<property name="test">
<column name="test" />
</property>
again right (just want to conferm)?

thanks
aziz
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes that is right. Since your columns each are a part of your composite key you cannot update them independently, so you cannot define properties for them. A change in a composite key value means you are working with a different record, implying that you cannot ever update your table, only insert or delete values into it.
 
somu negamam
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Aziz Dhanani !

I am also having the same problem what u having, I checked with ur solution but its not working when we start tomcat server the error occured and i implemented hasecode() and equals() also

so, plz send ur mapping file and hashcode() and equals() or whole bean(model) file.

Thanks in Advance

Shanmugam
 
somu negamam
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Aziz Dhanani !

I am also having the same problem what u having, I checked with ur solution but its not working when we start tomcat server the error occured and i implemented hasecode() and equals() also

so, plz send ur mapping file and hashcode() and equals() or whole bean(model) file.

Thanks in Advance

send it in somu_ngm@rediffmail.com id plzzzzzzzzzzzzzzzzzzzzz

Shanmugam
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic