Hey man i was able to simulate the scenario.
However i have used XML hope you can have appropriate anno.
It is as per table per subclass
One patient can have Many address. as i said earlier you can do that by making many2many unique.
One more thing there is one more table added for person's address mapping hope your manager is ok with that.
here is the code.
Code:
package com.akash.model;
import java.util.Set;
public class Person {
private int personId;
private Set<Address> addresses;
public int getPersonId() {
return personId;
}
public void setPersonId(int personId) {
this.personId = personId;
}
public Set<Address> getAddresses() {
return addresses;
}
public void setAddresses(Set<Address> addresses) {
this.addresses = addresses;
}
@Override
public String toString() {
return "Person [personId=" + personId + ", addresses=" + addresses
+ "]";
}
}
Code:
/** @author Akash Mittal
@version 1.0
Project:Temp
Package:com.akash.model
File Nam:Address.java
Last Updated 17-Apr-2013
Purpose:
*/
package com.akash.model;
public class Address {
private int aid;
private String street;
private String city;
public int getAid() {
return aid;
}
public void setAid(int aid) {
this.aid = aid;
}
public String getStreet() {
return street;
}
public void setStreet(String street) {
this.street = street;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
/**
* @param aid
* @param street
* @param city
*/
public Address(int aid, String street, String city) {
super();
this.aid = aid;
this.street = street;
this.city = city;
}
@Override
public String toString() {
return "Address [aid=" + aid + ", street=" + street + ", city="
+ city + "]";
}
}
Code:
/** @author Akash Mittal
@version 1.0
Project:Temp
Package:com.akash.model
File Nam:Patient.java
Last Updated 17-Apr-2013
Purpose:
*/
package com.akash.model;
public class Patient extends Person {
private int patientId;
public int getPatientId() {
return patientId;
}
public void setPatientId(int patientId) {
this.patientId = patientId;
}
@Override
public String toString() {
return "Patient [patientId=" + patientId + ", getPersonId()="
+ getPersonId() + ", getAddresses()=" + getAddresses() + "]";
}
}
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated Apr 17, 2013 10:12:02 AM by Hibernate Tools 3.4.0.CR1 -->
<hibernate-mapping>
<class name="com.akash.model.Person" table="PERSON">
<id name="personId" type="int">
<column name="PERSONID" />
<generator class="assigned" />
</id>
<set name="addresses" table="PATIENTS_ADDRESS" cascade="all">
<key>
<column name="PERSONID" />
</key>
<many-to-many class="com.akash.model.Address" column="AID" unique="true"/>
</set>
<joined-subclass name="com.akash.model.Patient">
<key>
<column name="PERSONID" />
</key>
<property name="patientId" type="int">
<column name="PATIENTID" />
</property>
</joined-subclass>
</class>
</hibernate-mapping>
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated Apr 17, 2013 10:12:02 AM by Hibernate Tools 3.4.0.CR1 -->
<hibernate-mapping>
<class name="com.akash.model.Address" table="ADDRESS">
<id name="aid" type="int">
<column name="AID" />
<generator class="assigned" />
</id>
<property name="street" type="java.lang.String">
<column name="STREET" />
</property>
<property name="city" type="java.lang.String">
<column name="CITY" />
</property>
</class>
</hibernate-mapping>
Code:
/** @author Akash Mittal
@version 1.0
Project:Temp
Package:com.akash.model
File Nam:MainClient.java
Last Updated 17-Apr-2013
Purpose:
*/
package com.akash.model;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;
import com.akash.util.Hibernate4Util;
public class MainClient {
public static void main(String[] args) {
Hibernate4Util.currentSession().beginTransaction();
Set<Address> addressSet = new HashSet<Address>();
addressSet.add(new Address(12,"Noida","UP"));
addressSet.add(new Address(13,"Ghaziabad","UP"));
addressSet.add(new Address(14,"Viashali","UP"));
Patient patient = new Patient();
patient.setPatientId(13);
patient.setPersonId(14);
patient.setAddresses(addressSet);
Hibernate4Util.currentSession().save(patient);
Hibernate4Util.currentSession().getTransaction().commit();
List<Patient> results = Hibernate4Util.currentSession().createQuery("from Patient ").list();
for(Patient p : results)
System.out.println(p);
}
}