Hibernate Tutorial

Hibernate Table Per Concrete Class using Implicit Polymorphism

Table per concrete class using implicit polymorphism:

Three tables are required. There will not be any relationship between these tables. Dis-Advantage of this would be, tough to write join queries between them.
Mapping
Book.hbm
<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE hibernate-mapping 
    PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN" 
    "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
 
<hibernate-mapping> 
    <class name="com.candidjava.hibernate.Book" table="book" > 
        <id name="id" type="integer" unsaved-value="0">
            <generator  class="increment"/>
        </id>
        <property name="title" type="string" column="title"/>
        <property name="author" type="string" column="author"/>
        <property name="cost" type="double" column="cost"/> 
    </class>
    
    <class  name="com.candidjava.hibernate.SpecialEditionBook" table="sbook">
        <id name="id" type="integer" unsaved-value="0">
            <generator  class="increment"/>
        </id>
        <property name="title" type="string" column="title"/>
        <property name="author" type="string" column="author"/>
        <property name="cost" type="double" column="cost"/> 
     	<property name="newfeatures" type="string" column="features"/>
    </class>
    
    <class  name="com.candidjava.hibernate.InternationalBook" table="ibook">
        <id name="id" type="integer" unsaved-value="0">
            <generator  class="increment"/>
        </id>
        <property name="title" type="string" column="title"/>
        <property name="author" type="string" column="author"/>
        <property name="cost" type="double" column="cost"/> 
        
    <property name="languages" type="string" column="languages"/>
        <property name="region" type="int" column="region"/>
    </class> 
</hibernate-mapping>

Inserting record into Table per concrete class

public void insertBook(Book bk) {
    try {
      Session s = getSession();
      Transaction transaction = s.beginTransaction();
      s.save(bk);
      transaction.commit();
    } catch (HibernateException e) {
      e.printStackTrace();
    }
  }
BookDao m=new BookDao();
    
    Book bk=new Book();
    bk.setAuthor("raman");
    bk.setCost(60);
    bk.setTitle("mysql");
    
    
    InternationalBook ib=new InternationalBook();
    ib.setAuthor("Candid");
    ib.setCost(70);
    ib.setTitle("mysql");
    ib.setLanguages("english");
    ib.setRegion(12);
    
    SpecialEditionBook sb=new SpecialEditionBook();
    sb.setAuthor("surendar");
    sb.setCost(90);
    sb.setTitle("j2ee");
    sb.setNewfeatures("angular");
    
    m.insertBook(bk);
    m.insertBook(ib);
    m.insertBook(sb);

Retrieving or getting record from Table per concrete class

public Book getBook(int id) {
    Book sd = null;
    try {
      Session s = getSession();
      sd = (Book) s.get(Book.class, id);
    } catch (HibernateException e) {
      System.out.println(e.getMessage());
    }
    return sd;
  }
BookDao ms=new BookDao();
    
    Book b=ms.getBook(2);
    
    System.out.println(b.getAuthor());
    System.out.println(b.getTitle());
    System.out.println(b.getCost());
    
    if(b instanceof InternationalBook)
    {
      InternationalBook ib=(InternationalBook) b;
      System.out.println(ib.getLanguages());
      System.out.println(ib.getRegion());
    }
    if(b instanceof SpecialEditionBook)
    {
      SpecialEditionBook sb=(SpecialEditionBook) b;
      System.out.println(sb.getNewfeatures());
      
    }
POJO
Book.java
package com.candidjava.hibernate;
 
public class Book {
  int id;
  String title;
  String author;
 
  double cost;
 
  public Book() {
  }
 
  public Book(String title, String author, double cost) {
    this.title = title;
    this.author = author;
 
    this.cost = cost;
  }
 
  public void setId(int id) {
    this.id = id;
  }
 
  public int getId() {
    return id;
  }
 
  public void setTitle(String title) {
    this.title = title;
  }
 
  public String getTitle() {
    return title;
  }
 
  public void setAuthor(String author) {
    this.author = author;
  }
 
  public String getAuthor() {
    return author;
  }
 
  public void setCost(double cost) {
    this.cost = cost;
  }
 
  public double getCost() {
    return cost;
  }
}
International Book
InternationalBook.java
package com.candidjava.hibernate;
 
public class InternationalBook extends Book {
 
  private String languages;
  private int region;
 
  public InternationalBook() {
  }
 
  public InternationalBook(String title, String author, double cost,
      String language, int region) {
    super(title, author, cost);
 
    languages = language;
    this.region = region;
  }
 
  public void setLanguages(String s) {
    languages = s;
  }
 
  public String getLanguages() {
    return languages;
  }
 
  public void setRegion(int i) {
    region = i;
  }
 
  public int getRegion() {
    return region;
  }
}
Special Edition Book
SpecialEditionBook.java
package com.candidjava.hibernate;
 
public class SpecialEditionBook extends Book {
 
  private String newfeatures;
 
  public SpecialEditionBook() {
  }
 
  public SpecialEditionBook(String title, String author, double cost,
      String features) {
    super(title, author, cost);
 
    newfeatures = features;
  }
 
  public void setNewfeatures(String s) {
    newfeatures = s;
  }
 
  public String getNewfeatures() {
    return newfeatures;
  }
}
Download