Yesterday we talked about many-to-one relationships. Today we will talk about one-to-many implementations.
Since multiple employees belong to one department.
We can also say that a department has multiple employees.
First, I would like to say that, whether it is multi-to-one or one-to-many, their common feature is that the two tables are associated. The essence of implementation is to find fields with common attributes (different names can be used, but the types are consistent during Association ).
For employee tables and department tables, we can use department numbers.
Next I will write a query for the number of persons in a department to illustrate the one-to-many implementation method.
1. Department object ing Class
PackageCom. Fish. testdao;
ImportJava. util. Set;
Public
ClassDepartment {
PrivateInteger
ID;
PrivateString
Name;
PrivateSet <employee>
Employee; // we can put all employees in a department into this set. By then, we only need to traverse this set and we will know who has a department.
PublicSet <employee> getemployee (){
Return
Employee;
}
Public
VoidSetemployee (set <employee> employee ){
This. Employee = employee;
}
PublicInteger GETID (){
Return
ID;
}
Public
VoidSetid (integer ID ){
This. ID = ID;
}
PublicString getname (){
Return
Name;
}
Public
VoidSetname (string name ){
This. Name = Name;
}
}
2. Employee object ing Class
PackageCom. Fish. testdao;
Public
ClassEmployee {
PrivateInteger
ID;
PrivateString
Name;
PrivateDepartment
Department; // make a foreign key here
PublicInteger GETID (){
Return
ID;
}
Public
VoidSetid (integer ID ){
This. ID = ID;
}
PublicString getname (){
Return
Name;
}
Public
VoidSetname (string name ){
This. Name = Name;
}
PublicDepartment getdepartment (){
Return
Department;
}
Public
VoidSetdepartment (Department ){
This. Department = Department;
}
}
3. Department XML
<? XML
Version = "1.0"
Encoding = "UTF-8"?>
<! Doctype
Hibernate-mapping public
"-// Hibernate/hibernatemapping DTD 3.0 // en"
Http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd>
<Hibernate-mapping>
<Class
Name = "com. Fish. testdao. Department">
<ID
Name = "ID"
Type = "integer">
<Generator
Class = "increment"> </generator>
</ID>
<Property
Name = "name"> </property>
<Set
Name = "employee">
<Key
Column = "department"> </key>
<One-to-least
Class = "com. Fish. testdao. Employee"/>
</Set>
</Class>
</Hibernate-mapping>
* Note <set name = "employee"> This set encapsulates the set attribute in the department class, which is consistent with that in the class.
<Keycolumn = "department"> </key> // This attribute is associated with the employee table.
<One-to-manyclass = "com. Fish. testdao. Employee"/> // The employee table is operated.
</Set>
4. Employee XML
<? XML
Version = "1.0"
Encoding = "UTF-8"?>
<! Doctype
Hibernate-mapping public
"-// Hibernate/hibernatemapping DTD 3.0 // en"
Http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd>
<Hibernate-mapping>
<Class
Name = "com. Fish. testdao. Employee"
>
<ID
Name = "ID"
Column = "ID" type = "integer">
<Generator
Class = "increment"> </generator>
</ID>
<Property
Name = "name"
> </Property>
<Role-to-one
Name = "department"
> </Allow-to-one>
</Class>
</Hibernate-mapping>
Let's write a test class
PackageCom. Fish. domain;
ImportJava. util. List;
ImportOrg. hibernate. query;
ImportOrg. hibernate. Session;
ImportOrg. hibernate. sessionfactory;
ImportOrg. hibernate. cfg. configuration;
ImportCom. Fish. testdao. Department;
ImportCom. Fish. testdao. employee;
Public
ClassTest2 {
// Write a template to get the session
Public
StaticSession getmysession (){
Configuration configuration =NewConfiguration ();
Configuration. Configure ("hibernate. cfg. xml ");
Sessionfactory factory = configuration. buildsessionfactory ();
Session session = factory. opensession ();
ReturnSession;
}
// This is a search for the number of employees in the department
Public
Static voidQuery (){
String hql = "fromdepartment ";
Session session =Getmysession();
Query query = session. createquery (hql );
List <Department> List = query. List ();
For(Department I: List ){
System.Out. Println (I. getname () + "");
For(Employee J: I. getemployee ()){
System.Out. Print (J. getname ());
}
System.Out. Println ("");
System.Out. Println ("");
}
}
Public
Static voidMain (string [] ARGs ){
Query();
}
}