-->
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: general: OR mapping in hibernate for composit primary keys
PostPosted: Fri Jan 07, 2005 11:17 am 
Newbie

Joined: Fri Jan 07, 2005 10:57 am
Posts: 3
Hi forum,
I am new to hibernate and want to port an application which works fine in EJB 2.1/ CMP. I browsed the documentation about compound-id and compound-index and the forum, but found nothing that could bring enlightment.

The scenario is like the following:
Three tables language, company and employee which have composite keys. I want to map this relational tables in hibernate. I REALLY want to do it this way no other scenario.

The tables company and employee have composit primary keys.
table language:
int lan_id (PK)
text lan_name
bool lan active

table company:
int com_id (PK)
int lan_id (PK)
text name

table employee:
int emp_id (PK)
int com_id (PK)
int lan_id (PK)
text name
int status

Now I want to map the employee business modell to hibernate. Please help. I currently experiment with a EmpPK class, but I am not sure if this is the right approach...

The mapping is like the following:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 2.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
<hibernate-mapping package="mypetstore.model.businessobject">
<class name="Emp" table="employee">
<composite-index class="mypetstore.model.businessobject.EmpPK" >
<key-property name="lanId" column="language" type="integer"/>
<key-property name="comId" column="company" type="integer"/>
<key-property name="empId" column="employee" type="integer"/>
</composite-index>

<property name="name" column="name"/>
<one-to-many name="com"
class="Com"
column="company"
cascade="none"/>
</class>
</hibernate-mapping>

Any help would be appreciated. Is there comprehensive documentation/ Howto/ Example available on how to work with composite keys.

Best Regards,
Mark


Top
 Profile  
 
 Post subject: composite primary keys not working in hibernate...
PostPosted: Sat Jan 08, 2005 4:47 am 
Newbie

Joined: Fri Jan 07, 2005 10:57 am
Posts: 3
Good Morning!

I figured that the problem above has something to do with the keywords "primary key composite-id class equals() hashCode()". Depending on how I google on those keywords I get thousands or hundreds of hits. Most of them are of no use. Many of them point to the not existant documentation (for this issue) in a rude way (RTFM).
On theserverside was a discussion about the same issue and they found no solution:
http://www.theserverside.com/discussions/thread.tss?thread_id=22638

What happens next?
If I am lucky I will find out next week that hibernate will cover this feature in version 4.x?


Top
 Profile  
 
 Post subject:
PostPosted: Sat Jan 08, 2005 3:15 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
there is a ton of docs for composite keys....what are you missing ? You don't say what's not working.....

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Sun Jan 09, 2005 5:03 am 
Newbie

Joined: Fri Jan 07, 2005 10:57 am
Posts: 3
Hi Max! Thanks for your reply. This is not a "I got this or that error/ trace" kind of question. What I am looking for is a more general form of advice, a best practice, a pattern, or something like that.
I do not need tons of documentation, the general idea would be fine. At my current level of understanding this breaks down into:
+ What classes do I need
+ are equals() and hashcode() requiered both in the business object and primary key class. This would look really ugly and I do not want those implementation specific stuff in my business objects (thats why I evaluate hibernate)
+ Is it possible to reduce the programming overhead for the composite keys with clever design. E.g. by use of base classes, use of xdoclet etc.
The link into the documentation would not hurt.
By the way how could the above mentioned problem on theserverside could be solved?


Top
 Profile  
 
 Post subject:
PostPosted: Sun Jan 09, 2005 5:27 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
first the tss post....the execption says:

Code:
composite-id class must override equals() and hashCode(): com.auctioninfo.model.PersonPK


That's pretty clear to me - PersonPK MUST implement equals/hashcode how else should hibernate be able to see if two keys are equal or different ? (some work has been done in H3 about removing this need for some types, but i'm not sure)

And what is ugly about having equals and hashcode in your business objects ? It is required in almost any case when dealing with Java 2 Collections. See http://www.hibernate.org/Documentation/ ... ndHashCode for more discussion on this not specific to hibernate question.

I don't use xdoclet for Hibernate so i can't say if it has something usefull for this.

If you don't want the extra class then use a unnamed composite-id and it will be your class that needs to do the identification - but then there are also limitations with that (documented in the ref docs and Hibernate in Action)

_________________
Max
Don't forget to rate


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.