91-9990449935 0120-4256464 |
Hibernate Table Per Hierarchy using xml fileBy this inheritance strategy, we can map the whole hierarchy by single table only. Here, an extra column (also known as discriminator column) is created in the table to identify the class. Let's understand the problem first. I want to map the whole hierarchy given below into one table of the database. There are three classes in this hierarchy. Employee is the super class for Regular_Employee and Contract_Employee classes. Let's see the mapping file for this hierarchy.
The table structure for this hierarchy is as shown below: Example of Table per class hierarchyIn 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) Add mapping of hbm file in 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.javaOutput:
download this inheritance mapping example (developed using Myeclipse IDE)
download this inheritance mapping example (developed using Eclipse IDE) Topics in Hibernate Inheritance MappingTable Per Hierarchy using xml fileTable Per Hierarchy using Annotation Table Per Concrete class using xml file Table Per Concrete class using Annotation Table Per Subclass using xml file Table Per Subclass using Annotation |