Hibernatede one-to-many mapping configuration

Source: Internet
Author: User

Hibernatede one -to-many mapping configuration

Take the company and employees as an example: The company is one, employees are more

The first step Create two entity classes, companies and employees

Write core configuration file Hibernate.cfg.xml

Write map configuration files Company.hbm.xml and Worker.hbm.xml

Step Two allow two entity classes to represent one another

(1 in the company entity class, it means having more than one employee, and writing the employee's collection as attributes to the company's entity class.

// within the company entity class, there are multiple employees, and a company has multiple employees.

//hibernate requires the use of collections to represent more data, using set Collection

Private set<worker> workers=new hashset<worker> ();

Public set<worker> getworkers () {

return workers;

}

public void Setworkers (set<worker> workers) {

This.workers = workers;

}

(2 ) in the employee entity class to indicate the owning company

- An employee can only belong to one company

// in the Employee entity class to represent the company, An employee can only belong to one company, write the company class as an attribute of the employee to the employee class.

Private company Company;

Public company Getcompany () {

Return company;

}

public void Setcompany (company company) {

This.company = Company;

}

Step three Configure mapping Relationships

(1 ) An entity class corresponds to a mapping file

(2 ) to complete the most basic configuration of the map

(3 in the mapping file, configure a one-to-many relationship

- in the company mapping file, configure all employees

1 Set Label ( Sub-label of Class label) represents a collection of employees

name Properties: the property value is written in the company entity class that represents the employee's set Collection Name

2 Key Label ( Sub-label of Set label)

column Property Value: Foreign Key Name

3 One-to-many Label ( Sub-label of Set label):

class Properties: the Entity class full path ( i.e. employee Class) that writes more than one side

Code:

<set name= "Workers" >

<key column= "c_w_id"/>

<one-to-many class= "entity. Worker "/>

</set>

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

using Many-to-one label ( the child label of the class tag), represents an employee-owned company

1 Name Properties: Because the company is used in the Employee entity class object representation, writing company name

2 class Properties: Company Full path

3 Column Properties: Foreign Key name (to be consistent with foreign key names in the company mapping file)

Code:

<many-to-one

Name= "Company" class= "Entity.company" column= "c_w_id" >

</many-to-one>

Fourth Step Create a core configuration file and introduce the mapping file into the core configuration file

<!-- Part III: Put the mapping file in the core configuration file Required--

<mapping resource= "Entity/company.hbm.xml"/>

Hibernatede one-to-many mapping configuration

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.