One-to-multiple hibernate implementation

Source: Internet
Author: User

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
Class
Department {

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
Void
Setemployee (set <employee> employee ){

This. Employee = employee;

}

PublicInteger GETID (){

Return
ID;

}

Public
Void
Setid (integer ID ){

This. ID = ID;

}

PublicString getname (){

Return
Name;

}

Public
Void
Setname (string name ){

This. Name = Name;

}

 

}

 

2. Employee object ing Class

 

PackageCom. Fish. testdao;

 

Public
Class
Employee {

PrivateInteger
ID;

PrivateString
Name;

PrivateDepartment
Department; // make a foreign key here

PublicInteger GETID (){

Return
ID;

}

Public
Void
Setid (integer ID ){

This. ID = ID;

}

PublicString getname (){

Return
Name;

}

Public
Void
Setname (string name ){

This. Name = Name;

}

PublicDepartment getdepartment (){

Return
Department;

}

Public
Void
Setdepartment (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
Class
Test2 {

// Write a template to get the session

Public
Static
Session 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 void
Query (){

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 void
Main (string [] ARGs ){

Query();

}

}

 

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.