Document directory
- One-to-multiple rules:
- Multiple-to-one rules:
Multiple-to-one rule: for example, the employee and department, multiple employees correspond to one department, which is called multiple-to-one;
One-to-multiple rules: for example, department and employee, a Department corresponds to multiple employees, which are called One-to-multiple rules;
One-to-multiple rules:
Public class Department {private int ID; private string name; private set <employee> EMPs; // This is a one-to-multiple getter setter ...}
public class Employee {private int id;private String name;getter setter...}
Set in department. HBM. xml:
<Set name = "EMPs"> <key column = "depart_id"> </key> <! -- Because the set attribute cannot be stored in this table, you need to create another table, so you need to specify the foreign key used to store the table of this set to connect to the ID attribute of the Department table --> <! -- Depart_id is the attribute that needs to be associated with the ID of the Department table in the employee table --> <one-to-learn class = "employee"/> <! -- Specify a one-to-multiple table --> </set>
After one-to-multiple rule settings, there is no change in the Department. The change is that there is an additional depart_id attribute in the employee;
Multiple-to-one rules:
public class Department {private int id;private String name;getter setter...}
Package org. xiazdong; public class employee {private int ID; private department depart; // This is multi-to-one. Use the Department object to reference departmentprivate string name; getter setter ...}
Set in employee. HBM. xml:
<Upload-to-one name = "depart" column = "depart_id"/> <! -- Set a depart_id in the employee to be associated with the Department's primary key -->
Instance:
Employee. Java
Package Org. xiazdong; public class employee {private int ID; private department depart; // This is a pair of multiple private string names; Public int GETID () {return ID ;} public void setid (int id) {This. id = ID;} Public String getname () {return name;} public void setname (string name) {This. name = Name;} public department getdepart () {return depart;} public void setdepart (Department depart) {This. depart = depart ;}}
Department. Java
Package Org. xiazdong; import Java. util. set; public class Department {private int ID; private string name; private set <employee> EMPs; // This is a one-to-multiple public set <employee> getemps () {return EMPs ;} public void setemps (set <employee> EMPs) {This. EMPs = EMPs;} public int GETID () {return ID;} public void setid (int id) {This. id = ID;} Public String getname () {return name;} public void setname (string name) {This. name = Name ;}}
Employee. HBM. xml
<?xml version="1.0"?><!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN""http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"><!-- Generated 2012-5-17 10:59:47 by Hibernate Tools 3.4.0.CR1 -->
Department. HBM. xml
<? XML version = "1.0"?> <! Doctype hibernate-mapping public "-// hibernate/hibernate mapping DTD 3.0 // en" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <! -- Generated 2012-5-17 10:59:47 by hibernate tools 3.4.0.cr1 -->
After execution, the table is:
Department
Employee
Test class:
package test;import org.hibernate.Session;import org.hibernate.Transaction;import org.hibernate.util.HibernateUtil;import org.xiazdong.Department;import org.xiazdong.Employee;public class Many2OneTest {public static void main(String[] args) {add();query();}static void query(){Session session = HibernateUtil.getSession();Transaction tx = session.beginTransaction();Department depart = (Department) session.get(Department.class, 6);Employee e = (Employee) session.get(Employee.class, 8);System.out.println(depart.getEmps());System.out.println(e.getDepart().getName());tx.commit();session.close();}static void add(){Department depart = new Department();depart.setName("depart");Employee e1 = new Employee();e1.setName("e1");e1.setDepart(depart);Employee e2 = new Employee();e2.setName("e2");e2.setDepart(depart);Session session = HibernateUtil.getSession();Transaction tx = session.beginTransaction();session.save(depart);session.save(e1);session.save(e2);tx.commit();session.close();}}