Here is my solution:
Basically father, mother and child are of type Person. So Person class has all common properties. Then create Family class that has associations with Father, Mother and Children.
Code:
package com.shyam.model;
/**
* @hibernate.class
* @author mutchaS
*
*/
public class Person {
private Long id;
private String name;
public Person() {}
/**
* @hibernate.id generator-class="native"
* @return
*/
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
/**
* @hibernate.property
*/
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
Code:
package com.shyam.model;
import java.util.Set;
/**
* @hibernate.class
* @author mutchaS
*
*/
public class Family {
private Long id;
private Person father;
private Person mother;
private Set children;
public Family() {}
/**
* @hibernate.id generator-class="native"
* @return
*/
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
/**
* @hibernate.many-to-one not-null="true" cascade="save-update" lazy="false"
* @return
*/
public Person getFather() {
return father;
}
public void setFather(Person father) {
this.father = father;
}
/**
* @hibernate.many-to-one not-null="true" cascade="save-update" lazy="false"
* @return
*/
public Person getMother() {
return mother;
}
public void setMother(Person mother) {
this.mother = mother;
}
/**
* @hibernate.set cascade="save-update, delete, delete-orphan"
* @hibernate.one-to-many class="com.shyam.model.Child"
* @hibernate.key column="FAMILY_ID" not-null="true" foreign-key="FK_FAMILY_ID"
* @hibernate.list-index column="CHILD_POSITION" base="1"
* @return
*/
public Set getChildren() {
return children;
}
public void setChildren(Set children) {
this.children = children;
}
}
Code:
package com.shyam.model;
/**
* @hibernate.joined-subclass
* @hibernate.joined-subclass-key column="id"
* @author mutchaS
*
*/
public class Child extends Person {/*
private Family family;
*//**
* @hibernate.many-to-one not-null="true" cascade="save-update" lazy="false"
* @return
*//*
public Family getFamily() {
return family;
}
public void setFamily(Family family) {
this.family = family;
}*/
}
Here is the Person.hbm.cfgCode:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping
>
<class
name="com.shyam.model.Person"
>
<id
name="id"
column="id"
>
<!-- The generator-class attribute of @hibernate.id is deprecated, use the @hibernate.generator tag instead -->
<generator class="native">
</generator>
</id>
<property
name="name"
column="name"
>
</property>
<joined-subclass
name="com.shyam.model.Child"
>
<!-- @hibernate.joined-subclass-key tag is deprecated, use @hibernate.key instead -->
<key
column="id"
/>
</joined-subclass>
</class>
</hibernate-mapping>
Here is the Family.hbm.cfgCode:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping
>
<class
name="com.shyam.model.Family"
>
<id
name="id"
column="id"
>
<!-- The generator-class attribute of @hibernate.id is deprecated, use the @hibernate.generator tag instead -->
<generator class="native">
</generator>
</id>
<many-to-one
name="father"
not-null="true"
cascade="save-update"
lazy="false"
>
</many-to-one>
<many-to-one
name="mother"
not-null="true"
cascade="save-update"
lazy="false"
>
</many-to-one>
<set
name="children"
cascade="save-update, delete, delete-orphan"
>
<key
column="FAMILY_ID"
foreign-key="FK_FAMILY_ID"
not-null="true"
>
</key>
<one-to-many
class="com.shyam.model.Child"
/>
</set>
</class>
</hibernate-mapping>
Here is how I save the family
/
Code:
/First unit of work
Session session = HibernateUtil.getSessionFactory().openSession();
Transaction tx = session.beginTransaction();
Person father = new Person();
father.setName("father1");
Person mother = new Person();
mother.setName("mother1");
Family family = new Family();
family.setFather(father);
family.setMother(mother);
Set children = new HashSet();
Child child = new Child();
child.setName("child1");
children.add(child);
Child child2 = new Child();
child2.setName("child2");
children.add(child2);
family.setChildren(children);
//session.save(family);
tx.commit();
session.close();
read familyCode:
List list = (List)session.createCriteria(Family.class).list();
System.out.println("family: "+list.size());
for(Iterator it = list.iterator(); it.hasNext();){
family = (Family)it.next();
System.out.println("\nfather: "+family.getFather().getName());
System.out.println("mother: "+family.getMother().getName());
children = family.getChildren();
for(Iterator it2 = children.iterator(); it2.hasNext();){
Child ch = (Child)it2.next();
System.out.println("children: "+ch.getName());
}
}
and find family whereCode:
list = (List)session.createCriteria(Family.class)
.createAlias("father", "father")
.add(Restrictions.ilike("father.name", "father1", MatchMode.START))
.list();
System.out.println("family: "+list.size());
for(Iterator it = list.iterator(); it.hasNext();){
family = (Family)it.next();
System.out.println("\nfather: "+family.getFather().getName());
System.out.println("mother: "+family.getMother().getName());
children = family.getChildren();
for(Iterator it2 = children.iterator(); it2.hasNext();){
Child ch = (Child)it2.next();
System.out.println("children: "+ch.getName());
}
}
You can insert inverse relation between Child and Family.
[/code]