ORM (Object relational Mapping) object/Relationship Map
When it comes to Hibernate's relational mapping, you have to mention the ORM. So what is ORM, which is a persistent technology that establishes an object relationship between the object model and the relational database, by manipulating the JavaBean object to complete the operation of the database!
Note: ORM is conceptually not actually present, it can be understood as a programming idea, and hibernate framework is the realization of the idea of the product!
To configure the association of PO Objects (and data table-bound persisted objects)
From the above, the ORM can establish a correspondence between the object model and the database, and the establishment of the relationship in Hibernate can be configured through an XML file. Please refer to "Hibernate integration", which is the foundation of ORM Persistence technology implementation. With the configuration of this relationship, ORM persistence technology can be made possible!
From hibernate integration, we can configure the relationship between JavaBean and database tables But if the two tables have a primary external relationship such as: How should an employee's department be configured when there is a foreign key column with a department number in the employee table?
Solution 1:
Create an entity class according to the structure of the data table (the Employee entity class holds the department number without the department name)
Solution 2:
Add a reference to the Department entity class in the Employee entity class
From the two solutions above, it is obvious that the second solution is more plausible. What if the first scenario is to query employee information and display the employee's department name? But the type of the Department entity class in the second scenario does not have that type in our own defined type data table, how do we configure it? In this case, we need to configure the relationship of two entity classes!
Configure a multiple-to-one single-association relationship
The problems encountered above can be solved by a multiple-to-one single-connection configuration. The variables for the departmental entity classes are configured in the XML file configuration of the employee entity class as follows:
<Name= "Department entity class variable" column= "Name of the foreign key column of the associated department table in the employee table" Class= "The full name of the departmental entity class (for example: cn.wz.entity.Dept)"/>
Where the <many-to-one> element establishes a mapping between the employee table and the main façade of the departmental table. It mainly contains the following properties:
- Name: Sets the property name of the persisted class, where the attributes of the departmental entity class variable are referenced in the Employee entity class.
- Column: Sets the foreign key of the table that corresponds to the properties of the persisted class, which is the foreign key for the employee table.
- Class: Sets the type of the property for the persisted class, which is the type of the department entity class.
Configure two-way, one-to-many associations
If you want to save its associated departmental entity class in the Employee entity class and save the associated employee (multiple) in the Department entity class, you can configure a two-way, one-to-many association
On the basis of configuring a multiple-to-one single association, add the properties of the employee entity class collection in the departmental entity class and add the following configuration to the file in its XML configuration file:
<name= "Reference the attribute name of the Employee entity class Collection " ><keycolumn= "The foreign key column name in the employee table that is associated with the Department table ></key> <one-to-many class= "Full name of the Employee entity class (for example: CN.WZ.ENTITY.EMP)"/> </set>
The Name property of the Set element: Sets the property name of the persisted class. Here is the name of the property referencing the collection of employee entity classes.
A set element also contains two child elements:
- Key element: The Column property sets the foreign key for the table corresponding to the persisted class to which it is associated, and here is the foreign key column name associated with the department table in the employee table
- Ons-to-many element: The Class property is set with its associated persistence class. Here is the employee entity class.
Configure one-way many-to-many associations
Configuring a single Many-to-many association is similar to configuring a one-to-many association by using the set element, where an employee (EMP) and a project are used for example
If you save multiple projects in the employee table that they participate in
The code in the employee entity class is as follows:
Public Implements java.io.serializable{ // private set<project> projects=New hashset<project> (0); // omitting constructs in the EMP class // }
Add the following configuration to its XML configuration file
<name= "Projects" table= "Proemp" > <key column= "Rempid"/ > <many-to-many class</set>
which
The table property of the Set element specifies that the name of the relationship table is Project
The column property of the Set child key element specifies the foreign key rempid of the Proemp table, which is used to refer to the EMP table
The class property of the Many-to-many child element specifies that the project object is stored in the Projects collection, and the Cloumn property specifies the foreign key rprojectid of the Proemp table to reference the project table
Configure bidirectional many-to-many associations
Configure bidirectional many-to-many associations as above simply swap the rempid and Rprojectid locations
Note: When using many-to-many associations, Hibernate automatically creates a proemp table in the database to connect the employee and project tables with only two columns Rempid and Rprojectid and Rempid as the foreign key for the employee table, and Rprojectid as the foreign key for the departmental table
Hibernate Association Mappings