hibernate中的多对一装备有两种状况:
一种是子表的外键对应主表的主键id。另一种是子表的外键对应主表的非主键字段,通常是一个仅有的字段。(常见的场景是订单主表跟订单产品明细表,主表跟子表都是用id作为主键,可是两者之间通过orderNo(订单号)来相关)下面依据这两种状况,简略介绍一下hibernate映射文件的装备。第一种状况,假设主表是部分表(department),表是职工表(employee)。domain类如下:Java代码 保藏代码
package com.xigua.domain; public class Department { /** * 主键id */ private Long id; /** * 部分称号 */ private String departName; public Long getId() { return id; } public void setId(Long id) { this.id = id; } public String getDepartName() { return departName; } public void setDepartName(String departName) { this.departName = departName; } } Java代码 保藏代码package com.xigua.domain; /** * 职工 * @author Administrator * */ public class Employee { /** * 主键id */ private Long id; /** * 职工称号 */ private String empName; /** * 部分目标 */ private Department department; public Long getId() { return id; } public void setId(Long id) { this.id = id; } public String getEmpName() { return empName; } public void setEmpName(String empName) { this.empName = empName; } public Department getDepartment() { return department; } public void setDepartment(Department department) { this.department = department; } } 对应的hibernate映射文件如下:Xml代码 保藏代码"-//Hibernate/Hibernate Mapping DTD 3.0//EN" "D:\tools\hibernate\dtd\hibernate-mapping-3.0.dtd"> Xml代码 保藏代码"-//Hibernate/Hibernate Mapping DTD 3.0//EN" "D:\tools\hibernate\dtd\hibernate-mapping-3.0.dtd"> 通过上述的装备以后,就将Employee跟Department进行了多对一绑定了。可敲下面的代码验证:Java代码 保藏代码package com.xigua.test; import org.hibernate.Session; import org.hibernate.Transaction; import com.xigua.domain.Department; import com.xigua.domain.Employee; import com.xigua.utils.HibernateUtil; public class Test1 { public static void main(String args[]) { save(); query(); } public static void save() { Session session = null; Transaction tx = null; try{ session = HibernateUtil.getSession(); tx = session.beginTransaction(); Department depart = new Department(); depart.setDepartName("dev"); Employee emp1 = new Employee(); emp1.setEmpName("xigua1"); // 级联保留职工的部分 emp1.setDepartment(depart); session.save(depart); session.save(emp1); tx.commit(); }catch(Exception e) { if(tx != null) { tx.rollback(); } throw e; }finally { if(session != null) { session.close(); } } } public static void query() { Session session = null; try{ session = HibernateUtil.getSession(); Employee emp = (Employee) session.get(Employee.class, 1L); System.out.println(emp.getEmpName()); // 级联检查职工对应的部分 Department depart = emp.getDepartment(); System.out.println(depart.getDepartName()); }catch(Exception e) { throw e; }finally { if(session != null) { session.close(); } } } } 假如想在Department目标中也绑定一对多联系,则需求修改Department.java类跟对应的hibernate映射文件。Department.java类中增加一个Set调集目标,对应的hibernate文件中也装备一个标签,详细见下面的代码。增加Set调集目标后的Department.java类:Java代码 保藏代码package com.xigua.domain; import java.util.Set; public class Department { /** * 主键id */ private Long id; /** * 部分称号 */ private String departName; /** * set调集 存在该部分下的职工信息 */ public Setemps; public Long getId() { return id; } public void setId(Long id) { this.id = id; } public String getDepartName() { return departName; } public void setDepartName(String departName) { this.departName = departName; } public SetgetEmps() { return emps; } public void setEmps(Setemps) { this.emps = emps; } }