Inheritance and polymorphism are two basic concepts for object-oriented programming languages. Hibernate's inheritance ing can understand the inheritance relationship between two persistence classes.
Hibernate supports several inheritance ing policies. Whatever the inheritance ing policy, the Hibernate multi-state query can run well.
Assume there are four objects: Department, Employee, Skiller, and Sales. The four objects have the following relationships:
From the above relationship we can see that there is a N-1 relationship between the Employee and the Department. Skiller and Sales are both child classes of Employee. The code for these four classes is as follows:
Department
[Java]
Public class Department {
Private Integer id;
Private String name;
Private Set <Employee> employees; // correlation
// The setter and getter methods are omitted.
}
Employee
[Java]
Public class Employee {
Private Integer id;
Private String name;
Private Department department;
// The setter and getter methods are omitted.
}
Skiller
[Java]
Public class Skiller extends Employee {
Private String skiller;
// The setter and getter methods are omitted.
}
Sales
[Java]
Public class Sales extends Employee {
Private int success;
// The setter and getter methods are omitted.
}
I. Use the inheritance ing of subclass Elements
Under this inheritance ing policy, all instances of the entire inheritance tree will be saved in the same table. Because all instances of the parent class and subclass are saved in the same table, you need to add columns to the table, this column can be used to identify the instance of the class in which each record belongs-this column is called the validator.
In this inheritance ing policy, we need to use <subclass... /> element to map the persistence class of the subclass. Use <discrimainator... /> element to map the identifiers, and you also need to give <subclass... /> Add the discriminator-value attribute to the element to specify the value of the speaker for each class.
The ing file is as follows:
[Html]
<Hibernate-mapping package = "com. hibernate. domain">
<Class name = "Employee" table = "employee">
<Id name = "id" column = "employeeID">
<Generator class = "hilo"/>
</Id>
<! -- Ing reporter -->
<Discriminator column = "type" type = "string"/>
<Property name = "name" column = "employeeName"/>
<! -- Used to map N-1 associated entities, specifying the associated entity class as: Department, specifying the foreign key as: External mentid -->
<Your-to-one name = "department" column = "Your mentid"/>
<! -- Map the Child class Skiller of the Employee using the subclass element -->
<Subclass name = "Skiller" discriminator-value = "skiller">
<Property name = "skiller"/>
</Subclass>
<! -- Use the subclass element to map the sub-class Sales of Employee -->
<Subclass name = "Sales" discriminator-value = "sales">
<Property name = "success"/>
</Subclass>
</Class>
</Hibernate-mapping>
Here we only need to map the parent class. In this configuration file, the type column is specified. This column does not make any sense in this province. It only serves to distinguish the persistence class corresponding to each record. The value of type is Skiller, and that of type is Sales. Use the following Program Section to perform operations.
[Html]
Static void add (){
Session session = HibernateUtil. getSession ();
Transaction tx = session. beginTransaction ();
Department department = new Department ();
Department. setName ("department name1 ");
Employee emp1 = new Employee ();
Emp1.setName ("employy name1 ");
Emp1.setId (1 );
Skiller emp2 = new Skiller ();
Emp2.setSkiller ("Electrician ");
Emp2.setName ("employee name2 ");
Emp2.setDepartment (department); // establishes the association between objects.
Sales emp3 = new Sales ();
Emp3.settimeout (50 );
Emp3.setName ("employee name3 ");
Emp3.setDepartment (department); // establishes the association between objects.
Session. save (department );
Session. save (emp1 );
Session. save (emp2 );
Session. save (emp3 );
Tx. commit ();
Session. close ();
}
In the above section, hibernate will generate the following SQL statements:
[SQL] view plaincopyprint?
Hibernate: insert into department (departmentName) values (?)
Hibernate: insert into employee (employeeName, inclumentid, type) values (?, ?, 'Com. hibernate. domain. employee ')
Hibernate: insert into employee (employeeName, inclumentid, skiller, type) values (?, ?, ?, 'Skiller ')
Hibernate: insert into employee (employeeName, inclumentid, primary, type) values (?, ?, ?, 'Sales ')
In the second SQL statement, the value of type is com. hibernate. domain. Employee. This value can be understood as null. In the third SQL statement, the value of type is skiller, and the value of type in the fourth SQL statement is sales. Note that the values column in the third SQL statement has no value, and the fourth skiller column also has no value. Therefore, when using subclass to inherit the ing policy, the attribute ing fields added to the subclass must not have non-null constraints.
The table structure is as follows:
Use the following Program Section to perform query operations:
[Java]
Static void query (int empid ){
Session session = HibernateUtil. getSession ();
Employee emp = (Employee) session. get (Employee. class, empid );
}
Obtain the following SQL statement:
[SQL]
Hibernate: select employee0 _. employeeID as employeeID2_0 _, employee0 _. employeeName as employee3_2_0 _, employee0 _. when mentid as me4_2_0 _, employee0 _. skiller as skiller2_0 _, employee0 _. same as sell2_0 _, employee0 _. type as type2_0 _
From employee employee0 _ where employee0 _. employeeID =?
From the preceding SQL statement, we can see that it obtains all records from a table. This is a great benefit of this inheritance policy: Under this ing policy, all the data of the entire inheritance tree is stored in a table, so no matter what kind of query, regardless of the layer in the inheritance tree, the underlying database only needs to query in a table, which is very convenient and efficient.
Here we add a sentence to the preceding query code: System. out. println (emp. getClass ());
Assign values to empid in sequence: 1 and 2. The result is as follows:
1. class com. hibernate. domain. Employee
2. class com. hibernate. domain. Skiller
The above output shows that hibernate is able to process polymorphism queries very well.
I. Use inheritance ing of joined-subclass Elements
When this policy is used, the parent class instance is stored in the parent class table, while the Child class instance is stored in the parent class table and the Child class table. In this policy, the attributes common to the parent class and the subclass are stored in the parent class table, and the attributes unique to the subclass are saved in the subclass table, that is, a table of the parent class, subclass a table, and add a foreign key to the parent table in the subclass table.
Use joined-subclass to map this policy, and you need to use the <key.../> element for each subclass to map a common primary key. This primary key class will also refer to the primary key column of the parent table.
The ing file is as follows:
[Html]
<Hibernate-mapping package = "com. hibernate. domain">
<Class name = "Employee" table = "employee">
<Id name = "id" column = "employeeID">
<Generator class = "hilo"/>
</Id>
<Property name = "name" column = "employeeName"/>
<! -- Used to map N-1 associated entities, specifying the associated entity class as: Department, specifying the foreign key as: External mentid -->
<Your-to-one name = "department" column = "Your mentid"/>
<! -- Use the join-class element to map the Skill of the Employee class and the subclass -->
<Joined-subclass name = "Skiller">
<! -- The key element must be used to map the primary keys of the Parent and Child classes. -->
<Key column = "employee_id"/>
<Property name = "skiller"/>
</Joined-subclass>
<! -- Use the join-class element to map the Sales class of the Employee class and subclass -->
<Joined-subclass name = "Sales">
<! -- The key element must be used to map the primary keys of the Parent and Child classes. -->
<Key column = "employee_id"/>
<Property name = "success"/>
</Joined-subclass>
</Class>
</Hibernate-mapping>
Execute the add operation by adding the code above. The following result is displayed:
The SQL statement is as follows:
[SQL]
Hibernate: insert into department (departmentName) values (?)
Hibernate: insert into employee (employeeName, receivmentid) values (?, ?)
Hibernate: insert into employee (employeeName, receivmentid) values (?, ?)
Hibernate: insert into Skiller (skiller, employee_id) values (?, ?)
Hibernate: insert into employee (employeeName, receivmentid) values (?, ?)
Hibernate: insert into Sales (values, employee_id) values (?, ?)
From the preceding SQL statement, we can see that hibernate handles this inheritance ing policy. A table of the parent class. A table of the Child class and a table of the Child class reference the foreign key of the parent class to establish an association.
For queries that inherit the ing policy like this, hibernate uses the table connection method to obtain the information of the subclass table:
[SQL]
Hibernate: select employee0 _. employeeID as employeeID2_0 _, employee0 _. employeeName as employee2_2_0 _, employee0 _. when mentid as me3_2_0 _, employee0_1 _. skiller as skiller3_0 _, employee0_2 _. same as sell4_0 _, case
When employee0_1 _. employee_id is not null then 1
When employee0_2 _. employee_id is not null then 2
When employee0 _. employeeID is not null then 0 end as clazz_0 _
From employee employee0 _ left
Outer join Skiller employee0_1 _
On employee0 _. employeeID = employee0_1 _. employee_id
Left outer join Sales employee0_2 _
On employee0 _. employeeID = employee0_2 _. employee_id
Where employee0 _. employeeID =?
Therefore, when joined-subclass is used to inherit the ing policy, when the program queries a subclass instance, it needs to query across multiple Word tables. The complexity depends on the number of parent classes of the subclass. This will inevitably affect the query performance.
1. Use the inheritance ing of the union-subclass Element
In this way, the data of the parent class instance is stored in the parent table, and the data of the Child class instance is only stored in the Word Table, without any record in the attached table. In this inheritance ing policy, the child table has more fields than the parent table.
In addition, in this ing policy, you do not need to use the classifier or the <key.../> element to map a common primary key.
The ing file is as follows:
[Html]
<Hibernate-mapping package = "com. hibernate. domain">
<Class name = "Employee" table = "employee">
<Id name = "id" column = "employeeID">
<Generator class = "hilo"/>
</Id>
<Property name = "name" column = "employeeName"/>
<! -- Used to map N-1 associated entities, specifying the associated entity class as: Department, specifying the foreign key as: External mentid -->
<Your-to-one name = "department" column = "Your mentid"/>
<! -- Use the union-subclass element to map a subclass of the Employee class: Skiller -->
<Union-subclass name = "Skiller" table = "skiller">
<Property name = "skiller"/>
</Union-subclass>
<! -- Use the union-subclass element to map a subclass of the Employee class: Sales -->
<Union-subclass name = "Sales" table = "sales">
<Property name = "success"/>
</Union-subclass>
</Class>
</Hibernate-mapping> www.2cto.com
Note: In this ing policy, the persistence class cannot use the identity primary key generation policy.
The following two sub-classes are obtained through the add operation:
Using this ing policy, the data in the underlying database looks more consistent with the normal database design, and different entities are stored in different data tables.
Author: chenssy