In
our previous example, we have seen one table is created for Product class and
all it's sub classes.
@Inheritance(strategy=InheritanceType.TABLE_PER_CLASS)
public
class Product {
....
....
....
}
By
using "InheritanceType.TABLE_PER_CLASS" strategy, we can create one
table for sub class.
package myFirstHibernate; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.Inheritance; import javax.persistence.InheritanceType; @Entity @Inheritance(strategy=InheritanceType.TABLE_PER_CLASS) public class Product { @Id @GeneratedValue(strategy = GenerationType.TABLE) int id; private int noOfProducts; public int getNoOfProducts() { return noOfProducts; } public void setNoOfProducts(int noOfProducts) { this.noOfProducts = noOfProducts; } }
package myFirstHibernate; import javax.persistence.Entity; @Entity public class Laptop extends Product { private String brand; private String model; public String getBrand() { return brand; } public void setBrand(String brand) { this.brand = brand; } public String getModel() { return model; } public void setModel(String model) { this.model = model; } }
package myFirstHibernate; import javax.persistence.Entity; @Entity public class Book extends Product{ private String title; private String author; public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public String getAuthor() { return author; } public void setAuthor(String author) { this.author = author; } }
hibernate.cfg.xml
<?xml version='1.0' encoding='utf-8'?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <!-- Database Connection settings --> <property name="connection.driver_class">com.mysql.jdbc.Driver</property> <property name="connection.url">jdbc:mysql://localhost/sample</property> <property name="connection.username">root</property> <property name="connection.password">tiger</property> <!-- Enable the logging of all the generated SQL statements to the console --> <property name="show_sql">true</property> <!-- Format the generated SQL statement to make it more readable, --> <property name="format_sql">false</property> <!-- Hibernate will put comments inside all generated SQL statements to hint what’s the generated SQL trying to do --> <property name="use_sql_comments">false</property> <!-- This property makes Hibernate generate the appropriate SQL for the chosen database. --> <property name="dialect">org.hibernate.dialect.MySQLDialect</property> <!-- Drop and re-create the database schema on startup --> <property name="hbm2ddl.auto">create</property> <!-- mappings for annotated classes --> <mapping class="myFirstHibernate.Product" /> <mapping class="myFirstHibernate.Book" /> <mapping class="myFirstHibernate.Laptop" /> </session-factory> </hibernate-configuration>
package myFirstHibernate; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.boot.registry.StandardServiceRegistryBuilder; import org.hibernate.cfg.Configuration; public class TestInheritance { /* Step 1: Create session factory */ private static SessionFactory getSessionFactory() { Configuration configuration = new Configuration().configure(); StandardServiceRegistryBuilder builder = new StandardServiceRegistryBuilder(). applySettings(configuration.getProperties()); SessionFactory factory = configuration.buildSessionFactory(builder.build()); return factory; } public static void main(String args[]){ Product prod = new Product(); Book book = new Book(); Laptop laptop = new Laptop(); prod.setNoOfProducts(2234); book.setAuthor("eckhart tolle"); book.setNoOfProducts(1234); book.setTitle("The Power of Now"); laptop.setBrand("Dell"); laptop.setModel("Alienware 17 Laptop"); laptop.setNoOfProducts(1000); /* To persist data */ SessionFactory sessionFactory = getSessionFactory(); Session session = sessionFactory.openSession(); session.beginTransaction(); session.save(prod); session.save(book); session.save(laptop); session.getTransaction().commit(); session.close(); } }
Run
TestInheritance class, you will get output like below.
Hibernate: drop table if exists Book Hibernate: drop table if exists Laptop Hibernate: drop table if exists Product Hibernate: drop table if exists hibernate_sequences Hibernate: create table Book (id integer not null, noOfProducts integer not null, author varchar(255), title varchar(255), primary key (id)) Hibernate: create table Laptop (id integer not null, noOfProducts integer not null, brand varchar(255), model varchar(255), primary key (id)) Hibernate: create table Product (id integer not null, noOfProducts integer not null, primary key (id)) Hibernate: create table hibernate_sequences ( sequence_name varchar(255), sequence_next_hi_value integer ) Dec 22, 2014 2:11:40 PM org.hibernate.tool.hbm2ddl.SchemaExport execute INFO: HHH000230: Schema export complete Hibernate: select sequence_next_hi_value from hibernate_sequences where sequence_name = 'Product' for update Hibernate: insert into hibernate_sequences(sequence_name, sequence_next_hi_value) values('Product', ?) Hibernate: update hibernate_sequences set sequence_next_hi_value = ? where sequence_next_hi_value = ? and sequence_name = 'Product' Hibernate: insert into Product (noOfProducts, id) values (?, ?) Hibernate: insert into Book (noOfProducts, author, title, id) values (?, ?, ?, ?) Hibernate: insert into Laptop (noOfProducts, brand, model, id) values (?, ?, ?, ?)
MySQL
table structures like below.
mysql> describe product; +--------------+---------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +--------------+---------+------+-----+---------+-------+ | id | int(11) | NO | PRI | NULL | | | noOfProducts | int(11) | NO | | NULL | | +--------------+---------+------+-----+---------+-------+ 2 rows in set (0.03 sec) mysql> describe book; +--------------+--------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +--------------+--------------+------+-----+---------+-------+ | id | int(11) | NO | PRI | NULL | | | noOfProducts | int(11) | NO | | NULL | | | author | varchar(255) | YES | | NULL | | | title | varchar(255) | YES | | NULL | | +--------------+--------------+------+-----+---------+-------+ 4 rows in set (0.01 sec) mysql> describe laptop; +--------------+--------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +--------------+--------------+------+-----+---------+-------+ | id | int(11) | NO | PRI | NULL | | | noOfProducts | int(11) | NO | | NULL | | | brand | varchar(255) | YES | | NULL | | | model | varchar(255) | YES | | NULL | | +--------------+--------------+------+-----+---------+-------+ 4 rows in set (0.01 sec) mysql> select * from product; +----+--------------+ | id | noOfProducts | +----+--------------+ | 1 | 2234 | +----+--------------+ 1 row in set (0.00 sec) mysql> select * from book; +----+--------------+---------------+------------------+ | id | noOfProducts | author | title | +----+--------------+---------------+------------------+ | 2 | 1234 | eckhart tolle | The Power of Now | +----+--------------+---------------+------------------+ 1 row in set (0.00 sec) mysql> select * from laptop; +----+--------------+-------+---------------------+ | id | noOfProducts | brand | model | +----+--------------+-------+---------------------+ | 3 | 1000 | Dell | Alienware 17 Laptop | +----+--------------+-------+---------------------+ 1 row in set (0.00 sec)
No comments:
Post a Comment