Inheritance Mapping in Hibernate

flutheronioneyedSoftware and s/w Development

Dec 13, 2013 (3 years and 7 months ago)

94 views

Hibernate- Inheritance Mapping Page : 1
Srikanth Technologies
Inheritance Mapping in Hibernate
The following examples illustrate how to map inheritance in Hibernate.
Common Classes
These are the classes that are used in all examples.

Cricketer.java
package inh;
public class Cricketer {
private int cid;
private String name,country;
public Cricketer() {
}
public int getCid() {
return cid;
}
public void setCid(int cid) {
this.cid = cid;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getCountry() {
return country;
}
public void setCountry(String country) {
this.country = country;
}

}

Bowler.java
package inh;
public class Bowler extends Cricketer {
private int nowickets;
public Bowler() {
}

public int getNowickets() {
return nowickets;
}

public void setNowickets(int nowickets) {
this.nowickets = nowickets;
}

}

Hibernate- Inheritance Mapping Page : 2
Srikanth Technologies
Batsman.java
package inh;
public class Batsman extends Cricketer {
private int noruns;
public Batsman() {
}

public int getNoruns() {
return noruns;
}
public void setNoruns(int noruns) {
this.noruns = noruns;
}

}

CricketManager.java
package inh;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

public class CricketersManager {
public static void main(String[] args) {
Configuration c = new Configuration().configure();
SessionFactory sf =
new Configuration().configure().buildSessionFactory();
Session session = sf.openSession();
session.beginTransaction();

Batsman bat = new Batsman();
bat.setCid(1);
bat.setName("Sachin Tendulkar");
bat.setCountry("India");
bat.setNoruns(11222);
session.save(bat);

Bowler bow = new Bowler();
bow.setCid(2);
bow.setName("Anil Kumble");
bow.setCountry("India");
bow.setNowickets(450);
session.save(bow);
session.getTransaction().commit();
session.close();
sf.close();
}
}

Hibernate- Inheritance Mapping Page : 3
Srikanth Technologies
hibernate.cfg.xml
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- Database connection settings -->
<property
name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
<property
name="connection.url">jdbc:oracle:thin:@localhost:1521:XE</property>
<property name="connection.username">test</property>
<property name="connection.password">test</property>
<property name="connection.pool_size">1</property>
<property
name="dialect">org.hibernate.dialect.Oracle10gDialect</property>
<property name="current_session_context_class">thread</property>
<!-- Disable the second-level cache -->
<property
name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
<property name="show_sql">true</property>
<property name="hbm2ddl.auto">none</property>

<!—Change hbm file appropriately -->
<mapping resource="inh/AllCricketers.hbm.xml"/>
</session-factory>
</hibernate-configuration>

One Table Per Class Hierarchy
In this all classes including subclasses are mapped to a single table. Discriminator
column is used to identify the type of the entity.

Creation of required tables
create table AllCricketers
( cid number(5) primary key,
name varchar(30),
country varchar(30),
type char(1),
noruns number(5),
nowickets number(3)
);

AllCricketers.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="inh.Cricketer" table="ALLCRICKETERS">
<id name="cid" type="int" column="CID">
<generator class="assigned"/>
</id>
<discriminator column="TYPE" type="string"/>
<property name="name"/>
<property name="country"/>
<subclass name="inh.Batsman" discriminator-value="1">
<property name="noruns"/>
</subclass>
Hibernate- Inheritance Mapping Page : 4
Srikanth Technologies
<subclass name="inh.Bowler" discriminator-value="2">
<property name="nowickets"/>
</subclass>
</class>
</hibernate-mapping>

One Table Per Each Subclass
Each subclass maps to a different table and these tables refer to super class table
using foreign key.

Cricketer.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="inh.Cricketer" table="CRICKETER">
<id name="cid" type="int" column="CID">
<generator class="assigned"/>
</id>
<property name="name"/>
<property name="country"/>

<joined-subclass name="inh.Bowler" table="Bowler">
<key column="cid"/>
<property name="nowickets"/>
</joined-subclass>
<joined-subclass name="inh.Batsman" table="Batsman">
<key column="cid"/>
<property name="noruns"/>
</joined-subclass>
</class>
</hibernate-mapping>

Creation of required tables
create table Cricketer
( cid number(5) primary key,
name varchar(30),
country varchar(30)
);

create table Batsman
( cid number(5) references Cricketer(cid),
noruns number(5)
);


create table Bowler
( cid number(5) references Cricketer(cid),
nowickets number(3)
);

Hibernate- Inheritance Mapping Page : 5
Srikanth Technologies
One Table For Each Concrete Class
In this each concrete subclass is mapped to a separate table which contains a
column for each property including properties inherited from super class.

OnlyCricketer.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="inh.Cricketer" table="ONLYCRICKETER">
<id name="cid" type="int" column="CID">
<generator class="assigned"/>
</id>
<property name="name"/>
<property name="country"/>
<union-subclass name="inh.Batsman" table="ONLYBATSMAN">
<property name="noruns" column="noruns"/>
</union-subclass>
<union-subclass name="inh.Bowler" table="ONLYBOWLER">
<property name="nowickets" column="nowickets"/>
</union-subclass>
</class>
</hibernate-mapping>

Creation of required tables
create table OnlyCricketer
( cid number(5) primary key,
name varchar(30),
country varchar(30)
);

create table OnlyBatsman
(
cid number(5) primary key,
name varchar(30),
country varchar(30),
noruns number(5)
);


create table OnlyBowler
( cid number(5) primary key,
name varchar(30),
country varchar(30),
nowickets number(3)
);