Hibernate Tutorial

Hibernate Joined Subclasses Example Annotation

Hibernate Joined subclasses annotation:

Each subclass can also be mapped to its own table. This is also called table-per-subclass mapping strategy. An inherited state is retrieved by joining with the table of the superclass.
A discriminator column is not required for this mapping strategy. Each subclass must, however, declare a table column holding the object identifier.
POJO
Book.java
package com.candidjava.hibernate;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Inheritance;
import javax.persistence.InheritanceType;
import javax.persistence.Table;
@Entity
@Table(name = "booktype")
@Inheritance(strategy = InheritanceType.JOINED)
public class Book {
  private int id;
  private String title;
  private String author;
  private double cost;
  public void setId(int id) {
    this.id = id;
  }
  @Id
  @GeneratedValue(strategy = GenerationType.AUTO)
  @Column
  public int getId() {
    return id;
  }
  public void setTitle(String title) {
    this.title = title;
  }
  @Column
  public String getTitle() {
    return title;
  }
  public void setAuthor(String author) {
    this.author = author;
  }
  @Column
  public String getAuthor() {
    return author;
  }
  public void setCost(double cost) {
    this.cost = cost;
  }
  @Column
  public double getCost() {
    return cost;
  }
}

 

International Book
InternationalBook.java
package com.candidjava.hibernate;
import javax.persistence.Entity;
import javax.persistence.PrimaryKeyJoinColumn;
import javax.persistence.Table;
@Entity
@Table(name = "IntlBook")
@PrimaryKeyJoinColumn(name = "ID")
public class InternationalBook extends Book {
  private String languages;
  private int region;
  public InternationalBook() {
  }
  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;
import javax.persistence.Entity;
import javax.persistence.PrimaryKeyJoinColumn;
import javax.persistence.Table;
@Entity
@Table(name = "SplEdiBook")
@PrimaryKeyJoinColumn(name = "ID")
public class SpecialEditionBook extends Book {
  private String newfeatures;
  public SpecialEditionBook() {
  }
  public void setNewfeatures(String s) {
    newfeatures = s;
  }
  public String getNewfeatures() {
    return newfeatures;
  }
}

Inserting record into Joined Sub Classes

public void insertBook(Book bk) {
    try {
      Session s = getSession();
      Transaction transaction = s.beginTransaction();
      s.save(bk);
      transaction.commit();
    } catch (HibernateException e) {
      e.printStackTrace();
    }
  }

Test Inserting record in Joined Sub Classes

package com.candidjava.hibernate;
 
public class TestInsertBook {
  public static void main(String arg[]) {
    try {
      BookDao m = new BookDao();
 
      Book bk = new Book();
 
      bk.setAuthor("Surrendar");
      bk.setCost(760);
      bk.setTitle("oracle");
 
      InternationalBook ib = new InternationalBook();
      ib.setAuthor("sivaraman");
      ib.setCost(960);
      ib.setTitle("oracle");
      ib.setLanguages("tamil");
      ib.setRegion(12);
 
      SpecialEditionBook sb = new SpecialEditionBook();
      sb.setAuthor("surrendar");
      sb.setCost(550);
      sb.setTitle("j2ee");
      sb.setNewfeatures("Jquery");
 
      m.insertBook(bk);
      m.insertBook(ib);
      m.insertBook(sb);
    } catch (Exception e) {
      System.out.println(e);
    }
  }
 
}

Retrieving or getting record from Joined Sub Classes

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;
  }

Test to Retrieve or get record from Joined Sub Classes

package com.candidjava.hibernate;
 
public class TestGetBook {
  public static void main(String arg[]) {
    BookDao ms = new BookDao();
 
    Book b = ms.getBook(14);
 
    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());
 
    }
 
  }
 
}
Download