Hibernate one to many mapping configuration method (sharing)

Take the company and employees as an example: the company is one, and there are many employees

The first step is to create two entity classes, company and employee

Write the core configuration file hibernate cfg. xml

Write mapping profile company hbm. XML and worker hbm. xml

The second step is to make the two entity classes represent each other

(1) In the company entity class, it means that there are multiple employees. The employee collection is written into the company entity class as an attribute.

(2) In the employee entity category, it represents the company to which it belongs

-An employee can only belong to one company

Step 3 configure the mapping relationship

(1) An entity class corresponds to a mapping file

(2) Complete the basic configuration of the mapping

(3) In the mapping file, configure a one to many relationship

-In the company mapping file, configure all employees

1. The set tag (a sub tag of the class tag) represents the collection of employees

Name attribute: the attribute value is written in the name of the set set representing employees in the company entity class

2 key tag (sub tag of set tag)

Column attribute value: foreign key name

3 one to many tag (sub tag of set tag):

Class attribute: the full path of the entity class (i.e. employee class) of the party with more than one written in it

code:

-In the employee mapping file, configure the company to which it belongs

Use the many to one tag (a sub tag of the class tag) to indicate the company to which the employee belongs

1. Name attribute: because the employee entity class is represented by the company object, write the company name

2. Class attribute: company full path

3. Column attribute: foreign key name (to be consistent with the foreign key name in the company mapping file)

code:

Step 4: create the core configuration file and introduce the mapping file into the core configuration file

The above hibernate one to many mapping configuration method (sharing) is all the content shared by Xiaobian. I hope it can give you a reference and support more programming tips.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>