Hibernate Inheriatance mapping Table per class example annotation based


This strategy has many drawbacks (esp. with polymorphic queries and associations) explained in the JPA spec, the Hibernate reference documentation, Hibernate in Action, and many other places. Hibernate work around most of them implementing this strategy using SQL UNION queries.

This strategy supports one-to-many associations provided that they are bidirectional. This strategy does not support the IDENTITY generator strategy: the id has to be shared across several tables. Consequently, when using this strategy, you should not use AUTO nor IDENTITY.

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 = "book1")
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
public class Book {
	@Id
	@GeneratedValue(strategy = GenerationType.TABLE)
	int id;
	@Column(name = "title")
	String title;
	@Column(name = "author")
	String author;
	@Column(name = "cost")
	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;

import javax.persistence.AttributeOverride;
import javax.persistence.AttributeOverrides;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Table;

@Entity
@Table(name = "InternationalBook")
@AttributeOverrides({
		@AttributeOverride(name = "id", column = @Column(name = "id")),
		@AttributeOverride(name = "title", column = @Column(name = "title")),
		@AttributeOverride(name = "author", column = @Column(name = "author")),
		@AttributeOverride(name = "cost", column = @Column(name = "cost"))

})
public class InternationalBook extends Book {
	@Column(name = "language")
	private String languages;
	@Column(name = "region")
	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;

import javax.persistence.AttributeOverride;
import javax.persistence.AttributeOverrides;
import javax.persistence.Column;
import javax.persistence.Entity;

@Entity
@AttributeOverrides({
		@AttributeOverride(name = "id", column = @Column(name = "id")),
		@AttributeOverride(name = "title", column = @Column(name = "title")),
		@AttributeOverride(name = "author", column = @Column(name = "author")),
		@AttributeOverride(name = "cost", column = @Column(name = "cost"))
})
public class SpecialEditionBook extends Book {
	@Column(name = "newfeatures")
	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;
	}
}
Inserting record into Table per class annotation
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("sivaraman");
		bk.setCost(760);
		bk.setTitle("oracle");

		InternationalBook ib=new InternationalBook();
		ib.setAuthor("siva");
		ib.setCost(1000);
		ib.setTitle("Spring");
		ib.setLanguages("english");
		ib.setRegion(12);
		
		SpecialEditionBook sb=new SpecialEditionBook();
		sb.setAuthor("sivasamy");
		sb.setTitle("j2ee");
		sb.setNewfeatures("webservices");
		sb.setCost(1000);
		m.insertBook(bk);
		m.insertBook(ib);
		m.insertBook(sb);
Retrieving or getting record from Table per 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(1);
		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
Table per class annotation war
Table per class annotation zip













Related Post

Comments


©candidjava.com