Table Per Concrete class using xml file

In instance of Table Per Concrete class, there will be three tables in the database having no relations to one another. There are two different ways to delineate table with table per solid class strategy.

  • By association subclass element
  • By self making the table for each class

Let's comprehend what hierarchy order we are going to map.

Let's see how can we map this hierarchy by union-subclass element:

  1. <?xml version='1.0' encoding='UTF-8'?>  
  2. <!DOCTYPE hibernate-mapping PUBLIC  
  3.           "-//Hibernate/Hibernate Mapping DTD 5.3//EN"  
  4.           "http://hibernate.sourceforge.net/hibernate-mapping-5.3.dtd">  
  5.   
  6.   <hibernate-mapping>  
  7.   <class name="com.javacodegeeks.mypackage.Employee" table="emp122">  
  8.   <id name="id">  
  9.   <generator class="increment"></generator>  
  10.   </id>  
  11.            
  12.   <property name="name"></property>  
  13.             
  14.   <union-subclass name="com.javacodegeeks.mypackage.Regular_Employee" table="regemp122">  
  15.   <property name="salary"></property>  
  16.   <property name="bonus"></property>  
  17.   </union-subclass>  
  18.             
  19.   <union-subclass name="com.javacodegeeks.mypackage.Contract_Employee" table="contemp122">  
  20.   <property name="pay_per_hour"></property>  
  21.   <property name="contract_duration"></property>  
  22.   </union-subclass>  
  23.             
  24.   </class>  
  25.             
  26.   </hibernate-mapping>  

Example of Table per concrete class

In this example we are creating the three classes and provide mapping of these classes in the employee.hbm.xml file.

1) Create the Persistent classes

You need to create the persistent classes representing the inheritance. Let's create the three classes for the above hierarchy:

File: Employee.java
  1. package com.javacodegeeks.mypackage;  
  2.   
  3. public class Employee {  
  4. private int id;  
  5. private String name;  
  6.   
  7. //getters and setters  
  8. }  
File: Regular_Employee.java
  1. package com.javacodegeeks.mypackage;  
  2.   
  3. public class Regular_Employee extends Employee{  
  4. private float salary;  
  5. private int bonus;  
  6.   
  7. //getters and setters  
  8. }  
File: Contract_Employee.java
  1. package com.javacodegeeks.mypackage;  
  2.   
  3. public class Contract_Employee extends Employee{  
  4.     private float pay_per_hour;  
  5.     private String contract_duration;  
  6.   
  7. //getters and setters  
  8. }  

2) Create the mapping file for Persistent class

The mapping has been discussed above for the hierarchy.

File: employee.hbm.xml
  1. <?xml version='1.0' encoding='UTF-8'?>  
  2. <!DOCTYPE hibernate-mapping PUBLIC  
  3.           "-//Hibernate/Hibernate Mapping DTD 5.3//EN"  
  4.           "http://hibernate.sourceforge.net/hibernate-mapping-5.3.dtd">  
  5.   
  6.   
  7.   <hibernate-mapping>  
  8.   <class name="com.javacodegeeks.mypackage.Employee" table="emp122">  
  9.   <id name="id">  
  10.   <generator class="increment"></generator>  
  11.   </id>  
  12.            
  13.   <property name="name"></property>  
  14.             
  15.   <union-subclass name="com.javacodegeeks.mypackage.Regular_Employee" table="regemp122">  
  16.   <property name="salary"></property>  
  17.   <property name="bonus"></property>  
  18.   </union-subclass>  
  19.             
  20.   <union-subclass name="com.javacodegeeks.mypackage.Contract_Employee" table="contemp122">  
  21.   <property name="pay_per_hour"></property>  
  22.   <property name="contract_duration"></property>  
  23.   </union-subclass>  
  24.             
  25.   </class>  
  26.             
  27.   </hibernate-mapping>  

3) Add mapping of hbm file in configuration file

Open the hibernate.cgf.xml file, and add an entry of mapping resource like this:

  1. <mapping resource="employee.hbm.xml"/>  

Now the configuration file will look like this:

File: hibernate.cfg.xml
  1. <?xml version='1.0' encoding='UTF-8'?>  
  2. <!DOCTYPE hibernate-configuration PUBLIC  
  3.           "-//Hibernate/Hibernate Configuration DTD 5.3//EN"  
  4.           "http://hibernate.sourceforge.net/hibernate-configuration-5.3.dtd">  
  5.   
  6. <hibernate-configuration>  
  7.   
  8.     <session-factory>  
  9.         <property name="hbm2ddl.auto">update</property>  
  10.         <property name="dialect">org.hibernate.dialect.Oracle9Dialect</property>  
  11.         <property name="connection.url">jdbc:oracle:thin:@localhost:1521:xe</property>  
  12.         <property name="connection.username">system</property>  
  13.         <property name="connection.password">jtp</property>  
  14.         <property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>  
  15.     <mapping resource="employee.hbm.xml"/>  
  16.     </session-factory>  
  17.   
  18. </hibernate-configuration>  

The hbm2ddl.auto property is defined for creating automatic table in the database.


4) Create the class that stores the persistent object

In this class, we are simply storing the employee objects in the database.

File: StoreData.java
  1. package com.javacodegeeks.mypackage;  
  2.   
  3. import org.hibernate.Session;  
  4. import org.hibernate.SessionFactory;  
  5. import org.hibernate.Transaction;  
  6. import org.hibernate.boot.Metadata;  
  7. import org.hibernate.boot.MetadataSources;  
  8. import org.hibernate.boot.registry.StandardServiceRegistry;  
  9. import org.hibernate.boot.registry.StandardServiceRegistryBuilder;  
  10.   
  11. public class StoreData {  
  12.   
  13.     public static void main(String[] args) {  
  14.   
  15.     StandardServiceRegistry ssr=new StandardServiceRegistryBuilder().configure("hibernate.cfg.xml").build();  
  16.     Metadata meta=new MetadataSources(ssr).getMetadataBuilder().build();  
  17.       
  18.     SessionFactory factory=meta.getSessionFactoryBuilder().build();  
  19.     Session session=factory.openSession();  
  20.       
  21.     Transaction t=session.beginTransaction();  
  22.       
  23.     Employee e1=new Employee();    
  24.     e1.setName("Gaurav Chawla");    
  25.         
  26.     Regular_Employee e2=new Regular_Employee();    
  27.     e2.setName("Vivek Kumar");    
  28.     e2.setSalary(50000);    
  29.     e2.setBonus(5);    
  30.         
  31.     Contract_Employee e3=new Contract_Employee();    
  32.     e3.setName("Arjun Kumar");    
  33.     e3.setPay_per_hour(1000);    
  34.     e3.setContract_duration("15 hours");    
  35.         
  36.     session.persist(e1);    
  37.     session.persist(e2);    
  38.     session.persist(e3);    
  39.         
  40.     t.commit();    
  41.     session.close();    
  42.     System.out.println("success");    
  43. }     
  44. }   package com.javacodegeeks.mypackage; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.Transaction; import org.hibernate.boot.Metadata; import org.hibernate.boot.MetadataSources; import org.hibernate.boot.registry.StandardServiceRegistry; import org.hibernate.boot.registry.StandardServiceRegistryBuilder; public class StoreData { public static void main(String[] args) { StandardServiceRegistry ssr=new StandardServiceRegistryBuilder().configure("hibernate.cfg.xml").build(); Metadata meta=new MetadataSources(ssr).getMetadataBuilder().build(); SessionFactory factory=meta.getSessionFactoryBuilder().build(); Session session=factory.openSession(); Transaction t=session.beginTransaction(); Employee e1=new Employee(); e1.setName("Gaurav Chawla"); Regular_Employee e2=new Regular_Employee(); e2.setName("Vivek Kumar"); e2.setSalary(50000); e2.setBonus(5); Contract_Employee e3=new Contract_Employee(); e3.setName("Arjun Kumar"); e3.setPay_per_hour(1000); e3.setContract_duration("15 hours"); session.persist(e1); session.persist(e2); session.persist(e3); t.commit(); session.close(); System.out.println("success"); } }






javacodegeeks is optimized for learning.© javacodegeeks .
All Right Reserved and you agree to have read and accepted our term and condition


python