-->
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: Composite keys - Hibernate mapping problem
PostPosted: Fri Aug 04, 2006 1:13 pm 
Newbie

Joined: Thu Aug 03, 2006 7:29 pm
Posts: 3
I am a Hibernate newbie, with a problem mapping legacy db entities with composite keys.

I have a relational model with Hospital, Doctor, Patient

Hospital enrolls many Patients.
Hospital employs many Doctors.
Doctors manage (are assigned to) Patients. (many-to-many).
DoctorPatient table is a join table for the relationship above.

Schema (only needed columns listed):
Surrogate keys would have been great, but it is legacy db and I have to make do with existing schema.

CREATE TABLE Hospital (
h_id INTEGER PRIMARY KEY,
.....
);

CREATE TABLE Doctor (
h_id INTEGER,
d_id INTEGER,
.....
PRIMARY KEY (h_id, d_id),
FOREIGN KEY f1 (h_id) REFERENCES Hospital(h_id)
);

CREATE TABLE Patient (
h_id INTEGER,
p_id INTEGER,
.....
PRIMARY KEY (p_id, h_id),
FOREIGN KEY f2 (h_id) REFERENCES Hospital(h_id)
);

CREATE TABLE DoctorPatient (
h_id INTEGER,
d_id INTEGER,
p_id INTEGER,
.....
PRIMARY KEY (h_id, d_id, p_id),
FOREIGN KEY f3 (h_id, d_id) REFERENCES Doctor(h_id, d_id),
FOREIGN KEY f4 (h_id, p_id) REFERENCES Patient(h_id, p_id)
);

As you can see, h_id in DoctorPatient is shared by both doctor and patient.

Mapping for DoctorPatient (note that Doctor and Patient share a part of their primary keys: h_id):

<class name="DoctorPatient" table="DoctorPatient" lazy="true">
<composite-id name="id" class="DoctorPatientId">
<key-many-to-one name="doctor" class="Doctor">
<column name="h_id"></column>
<column name="d_id"></column>
</key-many-to-one>
<key-many-to-one name="patient" class="Patient">
<column name="h_id"></column>
<column name="p_id"></column>
</key-many-to-one>
</composite-id>
</class>

I get the following Hibernate error:
org.hibernate.MappingException: Repeated column in mapping for entity: com.healthhero.core.DoctorPatient column: h_id (should be mapped with insert="false" update="false")
at org.hibernate.mapping.PersistentClass.checkColumnDuplication(PersistentClass.java:504)
at org.hibernate.mapping.PersistentClass.checkColumnDuplication(PersistentClass.java:542)
at org.hibernate.mapping.PersistentClass.validate(PersistentClass.java:335)
at org.hibernate.mapping.RootClass.validate(RootClass.java:188)
at org.hibernate.cfg.Configuration.validate(Configuration.java:839)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1000)
at com.healthhero.core.util.HibernateUtilities.init(HibernateUtilities.java:89)
at com.healthhero.core.util.HibernateUtilities.<clinit>(HibernateUtilities.java:43)
at com.healthhero.core.TestHibernate.main(TestHibernate.java:18)
Exception in thread "main" java.lang.NullPointerException
at com.healthhero.core.TestHibernate.main(TestHibernate.java:19)

Any help is appreciated!

_________________
Satish Gudiboina


Top
 Profile  
 
 Post subject:
PostPosted: Sun Aug 06, 2006 7:38 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
Use formula instead of column for one of the key-many-to-ones:
Code:
<composite-id name="id" class="DoctorPatientId">
  <key-many-to-one name="doctor" class="Doctor">
    <column name="h_id"/>
    <column name="d_id"/>
  </key-many-to-one>
  <key-many-to-one name="patient" class="Patient">
    <formula>h_id</formula>
    <column name="p_id"/>
  </key-many-to-one>
</composite-id>

_________________
Code tags are your friend. Know them and use them.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 07, 2006 2:33 pm 
Newbie

Joined: Thu Aug 03, 2006 7:29 pm
Posts: 3
Thanks for the reply! However, key-many-to-one elements do not allow formula child elements, they have to be columns. Any ideas?

_________________
Satish Gudiboina


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 07, 2006 3:04 pm 
Expert
Expert

Joined: Tue Apr 25, 2006 12:04 pm
Posts: 260
Did you tried something like below

Code:
<class name="DoctorPatient">
   <composite-id name="id" class="DoctorPatientId">
     <key-property name="doctorId" column="d_id"/>
     <key-property name="patientId" column="p_id"/>
     <key-property name="hospitalId" column="h_id"/>
   </composite-id>

   <many-to-one name="doctor" class="Doctor" insert="false" update="false">
       <column name="h_id"/>
       <column name="d_id"/>
   </many-to-one>

   <many-to-one name="patient" class="Patient" insert="false" update="false">
       <column name="h_id"/>
       <column name="p_id"/>
   </many-to-one>
</class>


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 08, 2006 12:00 pm 
Newbie

Joined: Thu Aug 03, 2006 7:29 pm
Posts: 3
Thanks! That was helpful. Thanks tenwit, I learnt about formula.

_________________
Satish Gudiboina


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.