In Java, there can be an inheritance relationship between classes, but there is no inheritance in the database. Hibernate, however, is designed to reflect object-oriented relationships to the database, and Hibernate provides us with 3 scenarios:
The first to first inheritance system is placed in a table (that is, all the attributes of the parent class and the subclass are reflected in a table)
Second, each child class maps a table, and then the table corresponding to the parent class is associated with a primary key
Third, each specific class mapping a table
We assume there are three classes: Employee (employee), Skill (technical), Sales (sale)
Employees are divided into technical and sales staff types, technical and sales categories inherit employee class:
Employee.java:
Private String ID; private String name; Private Department depart;
Skill.java:
Private String skill; Defining technology types
Sales.java:
private int sell; Define Sales Quantity
The first: An inheritance system placed in a single table
(with pictures, no rose, maybe not too good-looking, forgive me)
On top of that, we mapped three classes in a table: Employee top, let's take a look at Employee.hbm.xml:
<?xml Version= "1.0"?> <! DOCTYPE hibernate-mapping Public "-//hibernate/hibernate mapping DTD 3.0//en" "Http://hibernate.sourceforge.net /hibernate-mapping-3.0.dtd ">
We define discriminator-value= "0" in the <class> tab to distinguish the attributes of the record, and then we add a label <discriminator column= "type" type= "int" />, meaning this field is mapped to the Type column in the database, and the type is int. Then we used two <subclass> tags to define two inheritance relationships, where <property> is the object for each inherited class, and the Name property maps to the corresponding column in the database.We write a test class: Many2oneextend.java
public class Many2oneextend {public static void main (string[] args) {add ();} static void Add () {Session s = null; Transaction tx = NULL; try{ Employee EMP1 = new Employee (); Emp1.setname ("EMP1 name"); Sales EMP2 = new sales (); Emp2.setname ("EMP2 name"); Emp2.setsell (+); Skill Emp3 = new Skill (); Emp3.setname ("Emp3 name"); Emp3.setskill ("I am doing Java development"); s = hibernateutil.getsession (); tx = S.begintransaction (); S.save (EMP1); S.save (EMP2); S.save (Emp3); Tx.commit (); } finally{ if (s!=null) { s.close (); } }}}
We then look at the table of employee in the following database:
As you can see, three classes are already mapped in a single table.
Second, each child class maps a table, and then the table corresponding to the parent class is associated with a primary key
First, we add an attribute to the Sales.java and Skill.java two classes:
Private String ID;
Then we edit the Employee.hbm.xml file:<?xml version= "1.0"?> <! DOCTYPE hibernate-mapping Public "-//hibernate/hibernate mapping DTD 3.0//en" "Http://hibernate.sourceforge.net /hibernate-mapping-3.0.dtd "> Here we are using the <joined-subclass> tagLet's write a test class to run: Many2oneextend
Package Cn.itcast.hibernate;import Java.util.hashset;import Java.util.set;import org.hibernate.session;import Org.hibernate.transaction;import Cn.itcast.hibernate.domain.employee;import Cn.itcast.hibernate.domain.Sales; Import Cn.itcast.hibernate.domain.skill;public class Many2oneextend {public static void main (string[] args) {add ();} static void Add () {Session s = null; Transaction tx = NULL; try{Employee EMP1 = new Employee (); Emp1.setname ("EMP1 name"); Sales EMP2 = new sales (); Emp2.setname ("EMP2 name"); Emp2.setsell (100); Skill Emp3 = new Skill (); Emp3.setname ("Emp3 name"); Emp3.setskill ("I am doing Java development"); s = hibernateutil.getsession (); tx = S.begintransaction (); S.save (EMP1); S.save (EMP2); S.save (Emp3); Tx.commit (); }finally{IF (s!=null) {s.close (); } } }}
Then, let's look at three database tables:
Third, each specific class mapping a tableIn this case, the employee class is considered an abstract class, and the sales and skill classes inherit all the attributes of the employee. So there will be only sales and skill tables, no employee tables.
Below we look at the Employee.hbm.xml file:
<?xml Version= "1.0"?> <! DOCTYPE hibernate-mapping Public "-//hibernate/hibernate mapping DTD 3.0//en" "Http://hibernate.sourceforge.net /hibernate-mapping-3.0.dtd ">
Here, our <class> tag defines a property: Abstract= "true", which sets the class to be abstract.In addition to the <class></class> tag, we define two pairs of <union-subclass></union-subclass> tags, which define two inheritance classes for each tag.
Let's write a test file: Many2oneextend.java
public class Many2oneextend {public static void main (string[] args) {add ();} static void Add () {Session s = null; Transaction tx = NULL; try{ Sales emp2 = new sales (); Emp2.setname ("EMP2 name"); Emp2.setsell (+); Skill Emp3 = new Skill (); Emp3.setname ("Emp3 name"); Emp3.setskill ("I am doing Java development"); s = hibernateutil.getsession (); tx = S.begintransaction (); S.save (EMP2); S.save (Emp3); Tx.commit (); } finally{ if (s!=null) { s.close (); } }}}
Then, let's look at the tables in the database:As you can see, we just got two tables: the skill table and the sales table
"SSH three frames" Hibernate basics 11th: Operations on inheritance Mappings