Hibernate Association ing and hibernate Association ing

Source: Internet
Author: User

Hibernate Association ing and hibernate Association ing
ORM (Object Relational Mapping) Object/relationship ing

   

Speaking of Hibernate's association ing, you must mention ORM. What is ORM? It is a persistence technology. It establishes Object Relationships between object models and relational databases and completes database operations by operating the JavaBean object!

Note: The concept of ORM does not actually exist. It can be understood as a programming idea, and The Hibernate framework is the implementation product of this idea!

Configure the association between Po objects (persistent objects bound to data tables)
   

As mentioned above, the orm can establish a correspondence between the object model and the database, while the establishment of this relationship in Hibernate can be configured through the xml file. For details, see hibernate integration. the establishment of this relationship is the foundation of the implementation of the orm persistence technology. With the configuration of this relationship, the ORM persistence technology can become possible!

From hibernate integration, we can configure the an correspondence with the database table. However, if the two tables have a primary-external relationship, for example: what should I do when an employee has a department that has a foreign key column of the Department number in the employee table?

Solution 1:
   

Create an entity class based on the structure of the data table (the employee entity class stores the Department ID without the Department name)

Solution 2:
   

Add a reference to the Department entity class to the employee entity class

   

From the above two solutions, it is clear that the second solution should be more reliable. If it is the first solution, what should I do if I query the employee information and display the employee's department name? However, in the second solution, the Department entity type does not exist in the data table of the type defined by ourselves. How can we configure this type? In this case, you need to configure the associations between the two entity classes!

Configure multiple-to-one Association
   

The problems encountered above can be solved through multiple-to-one association configuration. In the xml file configuration of the employee entity class, configure the Department entity class variables as follows:

<Role-to-one name = "department entity class variable" column = "name of the foreign key column associated with the Department table in the employee table" class = "full name of the department entity class (for example: cn. wz. entity. dept) "/>

The <employee-to-one> element establishes a ing between the employee table and the Department table. It mainly includes the following attributes:

Configure bidirectional one-to-Multiple Association
  

If you want to save the associated Department entity class in the employee entity class and save the associated employees (multiple) in the Department entity class, you can configure two-way one-to-Multiple Association.

Add the attributes of the employee entity class set in the department entity Class Based on the configuration of multiple-to-one association and add the following configuration in the xml configuration file:

<Set name = "reference property name of an employee entity class set"> <key column = "name of the foreign key column associated with the Department table in the employee table"> </key> <one- to-define class = "full name of employee entity class (for example: cn. wz. entity. emp) "/> </set>

Name attribute of the set element: set the attribute name of the persistence class. Here is the name of the property that references the set of employee entity classes.

The set element also contains two child elements:

Configure one-way, many-to-many Association
  

Configuring single-to-multiple associations is similar to configuring one-to-multiple associations. In this case, you can use the EMP and PROJECT for example.

For example, you can save multiple projects in the employee table.

The code in the employee entity class is as follows:

Public calss Emp implements java. io. serializable {// omit other attributes in the Emp class private Set <Project> projects = new HashSet <Project> (0 ); // omitting the constructor In the Emp class // omitting the getter and setter Methods}

Add the following configuration to the xml configuration file:

<set name=”projects”table=“PROEMP”>        <key column=”REMPID”/>        <many-to-many class=”cn.wz.entity.Project”column=”RProjectID”/> </set>

Where:

The table attribute of the set element specifies that the name of the relational table is PROJECT.

The column attribute of the key element of the set sub-element specifies the foreign key rempid of the proemp table, which is used to reference the EMP table.

The class attribute of the sub-element of allow-to-consume specifies that the Project object is stored in the projects set. The cloumn attribute specifies the foreign key RProjectID of the proemp table, which is used to reference the PROJECT table.

Configure bidirectional multiple-to-Multiple Association
  

Configure bidirectional multiple-to-Multiple Association. Just change the REMPID and RProjectID locations.

Note: when multiple-to-multiple associations are used, Hibernate automatically creates a PROEMP table in the database to connect to the employee table and project table. The table has only two columns of REMPID and RProjectID, And the REMPID column is the foreign key of the employee table and RProjectId. is the foreign key of the Department table

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.