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
|