Hibernatede one-to-multiple ing configuration, hibernatede ing
HibernatedeOne-to-multiple ing Configuration
Take the company and its employees as an example: the company is one with many employees
Step 1Create two entity classes, company and employee
Write the core configuration file hibernate. cfg. xml
Write Company ing configuration file Company. hbm. xmlAnd Worker. hbm. xml
Step 2Mutual representation of two entity classes
(1In the entity class of the company, it indicates that there are multiple employees. The set of employees is used as attributes and written into the entity class of the company.
//In the company entity class, it indicates that there are multiple employees, and a company has multiple employees.
// HibernateSetSet
Private Set <Worker> workers = new HashSet <Worker> ();
Public Set <Worker> getWorkers (){
Return workers;
}
Public void setWorkers (Set <Worker> workers ){
This. workers = workers;
}
(2) Indicates the company in the employee entity class.
-An employee can belong to only one company.
//Indicates the company in the employee entity class,An employee can belong to only one company,Take the company class as an employee attribute and write it into the employee class.
Private Company company;
Public Company getCompany (){
Return company;
}
Public void setCompany (Company company ){
This. company = company;
}
Step 3Configure Mappings
(1) An object class corresponds to a ing file.
(2) Complete the most basic configuration of the ing.
(3) In the ing file, configure one-to-multiple relationships.
-Configure all employees in the company ing File
1 setLabel (classTags)Indicates the set of employees.
NameAttribute:The property value is written in the Company Entity class to indicate the employee's setSet Name
2 keyTag (setTags)
ColumnProperty Value: foreign key name
3 one-to-leastTag (setTags ):
ClassAttribute:Write the full path of Multiple object classes (That is, employees)
Code:
<Set name = "workers">
<Key column = "c_w_id"/>
<One-to-define class = "entity. Worker"/>
</Set>
-In the employee ing file, configure the company
Use upload-to-oneLabel (classTags ),Indicates the company to which the employee belongs
1 nameAttribute: Because company is used in the employee entity classObject Representation, write companyName
2 classProperty: CompanyFull path
3 columnProperty: foreign key name (to be consistent with the foreign key name in the company ing file)
Code:
<Role-to-one
Name = "company" class = "entity. Company" column = "c_w_id">
</Role-to-one>
Step 4Create a core configuration file and introduce the ing file to the core configuration file.
<! --Part 3:Place the ing file in the core configuration fileRequired -->
<Mapping resource = "entity/Company. hbm. xml"/>