91-9990449935 0120-4256464 |
Table Per Subclass Example using xml fileIn case of Table Per Subclass, subclass mapped tables are related to parent class mapped table by primary key and foreign key relationship. The <joined-subclass> element of class is used to map the child class with parent using the primary key and foreign key relation. In this example, we are going to use hb2ddl.auto property to generate the table automatically. So we don't need to be worried about creating tables in the database. Let's see the hierarchy of classes that we are going to map. Let's see how can we map this hierarchy by joined-subclass element:
The table structure for each table will be as follows: Table structure for Employee classTable structure for Regular_Employee classTable structure for Contract_Employee classExample of Table per subclass classIn this example we are creating the three classes and provide mapping of these classes in the employee.hbm.xml file. 1) Create the Persistent classesYou need to create the persistent classes representing the inheritance. Let's create the three classes for the above hierarchy: File: Employee.java File: Regular_Employee.java File: Contract_Employee.java2) Create the mapping file for Persistent classThe mapping has been discussed above for the hierarchy. File: employee.hbm.xml3) create configuration fileOpen the hibernate.cgf.xml file, and add an entry of mapping resource like this: Now the configuration file will look like this: File: hibernate.cfg.xmlThe hbm2ddl.auto property is defined for creating automatic table in the database. 4) Create the class that stores the persistent objectIn this class, we are simply storing the employee objects in the database. File: StoreData.java
download this example developed using Myeclipse IDE
download this example developed using Eclipse IDE |