Thursday 17 May 2012

Inheritance Mapping

Hibernate Persistence classes are POJO classes. so they can participated in inheritance to give the advantage of re-usability  and extensibility.

Hibernate Software gives three approaches performing inheritance mapping to get reusability, Extensibility advantages of inheritance in different ways. they are 

1. Table Per Class Hierarchy
2. Table Per Sub Class
3. Table Per Concrete class

1.Table Per Class Hierarchy

all the classes of inheritance hierarchy will use single DB table by having discriminator column


The Logical values of the discriminator column are very useful to select specific POJO class related records separately from DB table even through the db table contains other POJO class related records.


In table per class hierarchy inheritance mapping <class> will be used to configure super class and sub class will be used to configure sub classes this subclass is <sub> of <class>


      ID      OFFER_TYPE         NAME        TYPE          PRICE     DISCOUNT        CDS
-------- -       ---------              ---------- -    ---------       ----------   ----------           ----------
       1              book                  xyz            core              159
       2              AE                     abc             j2ee              169         10
       3              SE                     ladkjsf          spic            1658                                5


In this Example Book.java is super class
and SpecialEditionBook and AnnaEditionBook is subclasses of Book.java

Book.java

public class Book implements Serializable{

private short id;
private String name;
private String type;
private BigDecimal price;

And Setter and getter Methods
}

SpecialEditionBook.java


public class SpecialEditionBook extends Book {
private byte cds;

       Setter and getter Methods
}

AnnEditionBook.java

public class AnnEditionBook extends Book {

private byte discount;
And Setter and getter Methods
}


Mapping file(book.hbm.xml)

<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"><!--  -->
<hibernate-mapping>
<class name="Book" table="book" discriminator-value="book">
<id name="id" column="id"></id>
<discriminator column="offer_type" type="string"></discriminator>
<property name="name" column="name"></property>
<property name="type" column="type"></property>
<property name="price" column="price"></property>
<subclass name="AnnEditionBook" extends="Book" discriminator-value="AE">
<property name="discount" column="discount"></property>
</subclass>
<subclass name="SpecialEditionBook" extends="Book" discriminator-value="SE">
<property name="cds" column="cds"></property>
</subclass>
</class>
</hibernate-mapping>

                                                  For Download the above code:


2.Table Per SubClass

In this model every pojo class of inheritance heirarchy will have its own database table

There is no necessity of discriminator column

In table per sub class model of inheritance mapping <class> will be used to configure parent POJO class and <joined-subclass> will be used to configure child POJO classes <joined-subclass> is the sub tag of <class>

table per subclass inheritance mapping model is most regular used inheritance mapping model in company level programming, because
a. It recognizes inheritance between POJO classes and gives reusability of properties  
b. it allows to design tables having relationships and satisfying DB designing Principles
c. Allows to apply not null constraint on all the columns of all the tables

Parent Table(book)

  ID NAME       TYPE            PRICE
---- ----------   ----------         ----------
   1     xyz         core               159
   2     abc         j2ee                169
   3    ladkjsf      spic               1658


Child Table(aebook)

 ID   DISCOUNT
--- ----------
  2         10

Child Table(sebook)


 ID        CDS
--- -   ---------
  3           5

POJO classes are same as above.

Mapping file(book.hbm.xml)

<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"><!--  -->
<hibernate-mapping>
<class name="Book" table="book">
<id name="id" column="id"></id>
<property name="name" column="name" length="10"></property>
<property name="type" column="type" length="10"></property>
<property name="price" column="price"></property>
<joined-subclass name="AnnEditionBook" table="aebook">
<key column="id"></key>
<property name="discount" column="discount"></property>
</joined-subclass>
<joined-subclass name="SpecialEditionBook" table="SEbooK">
<key column="id"></key>
<property name="cds" column="cds"></property>
</joined-subclass>
</class>
</hibernate-mapping>

Client.java

public class Client {
public static void main(String[] args) {
// TODO Auto-generated method stub
Configuration cfg=new Configuration();
cfg.configure();
SessionFactory sf=cfg.buildSessionFactory();
Session s=sf.openSession();
Transaction t=s.beginTransaction();
Book obj=new Book();
obj.setId((short) 1);
obj.setName("xyz");
obj.setPrice(new BigDecimal(159));
obj.setType("core");
s.save(obj);
System.out.println("Book is saved");
AnnEditionBook obj1=new AnnEditionBook();
obj1.setId((short) 2);
obj1.setName("abc");
obj1.setPrice(new BigDecimal(169));
obj1.setType("j2ee");
obj1.setDiscount((byte) 10);
s.save(obj1);
SpecialEditionBook obj2=new SpecialEditionBook();
obj2.setId((short) 3);
obj2.setName("ladkjsf");
obj2.setPrice(new BigDecimal(1658));
obj2.setType("spic");
obj2.setCds((byte) 5);
s.save(obj2);
t.commit();
s.close();
System.out.println("finished....");
}
}

For Download the above code:



3.Table Per Concreate Class

In this inheritance mapping even though POJO classes are there in the inheritance their tables will not participate in relationship and Every POJO class of inheritance hierarchy will use one separate table without having relationship with other table.

These Pojo classes willbe configured in hibernate mapping file as individual,independent, concrete classes without showing inheritance and without showing reusability of properties configuration

Pojo classes are same as above

book.hbm.xml(mapping file)


<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"><!--  -->
<hibernate-mapping>
<class name="Book" table="book">
<id name="id" column="id"></id>

<property name="name" column="name"></property>
<property name="type" column="type"></property>
<property name="price" column="price"></property>
</class>
<class name="AnnEditionBook" table="aebook">
<id name="id" column="id"></id>
<property name="name" column="name"></property>
<property name="type" column="type"></property>
<property name="price" column="price"></property>
<property name="discount" column="discount"></property>
</class>

<class name="SpecialEditionBook" table="sebook">
<id name="id" column="id"></id>
<property name="name" column="name"></property>
<property name="type" column="type"></property>
<property name="price" column="price"></property>
<property name="cds" column="cds"></property>
</class>
</hibernate-mapping>

client application(client.java)

public class Client {
public static void main(String[] args) {
// TODO Auto-generated method stub
Configuration cfg=new Configuration();
cfg.configure();
SessionFactory sf=cfg.buildSessionFactory();
Session s=sf.openSession();
Transaction t=s.beginTransaction();
Book obj=new Book();
obj.setId((short) 1);
obj.setName("xyz");
obj.setPrice(new BigDecimal(159));
obj.setType("core");
s.save(obj);
System.out.println("Book is saved");
AnnEditionBook obj1=new AnnEditionBook();
obj1.setId((short) 2);
obj1.setName("abc");
obj1.setPrice(new BigDecimal(169));
obj1.setType("j2ee");
obj1.setDiscount((byte) 10);
s.save(obj1);
SpecialEditionBook obj2=new SpecialEditionBook();
obj2.setId((short) 3);
obj2.setName("ladkjsf");
obj2.setPrice(new BigDecimal(1658));
obj2.setType("spic");
obj2.setCds((byte) 5);
s.save(obj2);
t.commit();
s.close();
System.out.println("finished....");
}
}
For Download the above code
















No comments:

Post a Comment