本文主要是介绍hibernate进阶之一对多和inverse属性,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
dept.java
package cn.itcast.b_one2Many;import java.util.HashSet;
import java.util.Set;public class Dept {private int deptId;private String deptName;// 【一对多】 部门对应的多个员工private Set<Employee> emps = new HashSet<Employee>();public int getDeptId() {return deptId;}public void setDeptId(int deptId) {this.deptId = deptId;}public String getDeptName() {return deptName;}public void setDeptName(String deptName) {this.deptName = deptName;}public Set<Employee> getEmps() {return emps;}public void setEmps(Set<Employee> emps) {this.emps = emps;}
}
Employee.java
package cn.itcast.b_one2Many;public class Employee {private int empId;private String empName;private double salary;// 【多对一】员工与部门private Dept dept;public int getEmpId() {return empId;}public void setEmpId(int empId) {this.empId = empId;}public String getEmpName() {return empName;}public void setEmpName(String empName) {this.empName = empName;}public double getSalary() {return salary;}public void setSalary(double salary) {this.salary = salary;}public Dept getDept() {return dept;}public void setDept(Dept dept) {this.dept = dept;}
}
Dept.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN""http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"><hibernate-mapping package="cn.itcast.b_one2Many"><class name="Dept" table="t_dept"><id name="deptId"><generator class="native"></generator></id> <property name="deptName" length="20"></property><!-- 一对多关联映射配置 (通过部门管理到员工)Dept 映射关键点:1. 指定 映射的集合属性: "emps"2. 集合属性对应的集合表: "t_employee"3. 集合表的外键字段 "t_employee. dept_id"4. 集合元素的类型inverse=false set集合映射的默认值; 表示有控制权--><set name="emps" cascade="save-update,delete" table="t_employee" inverse="false"> <!-- table="t_employee" --><key column="dept_id"></key><one-to-many class="Employee"/></set> </class>
</hibernate-mapping>
Employee.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN""http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"><hibernate-mapping package="cn.itcast.b_one2Many"><class name="Employee" table="t_employee"><id name="empId"><generator class="native"></generator></id> <property name="empName" length="20"></property><property name="salary" type="double"></property><!-- 多对一映射配置Employee 映射关键点:1. 映射的部门属性 : dept2. 映射的部门属性,对应的外键字段: dept_id3. 部门的类型--><many-to-one name="dept" column="dept_id" class="Dept"></many-to-one> </class>
</hibernate-mapping>
App1_save.java
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.classic.Session;
import org.junit.Test;public class App1_save {private static SessionFactory sf;static {sf = new Configuration().configure().addClass(Dept.class) .addClass(Employee.class) // 测试时候使用.buildSessionFactory();}// 保存, 部门方 【一的一方法操作】@Testpublic void save() {Session session = sf.openSession();session.beginTransaction();// 部门对象Dept dept = new Dept();dept.setDeptName("应用开发部");// 员工对象Employee emp_zs = new Employee();emp_zs.setEmpName("张三");Employee emp_ls = new Employee();emp_ls.setEmpName("李四");// 关系dept.getEmps().add(emp_zs);dept.getEmps().add(emp_ls);// 保存session.save(emp_zs);session.save(emp_ls);session.save(dept); // 保存部门,部门下所有的员工 session.getTransaction().commit();session.close();/*结果* inverse = falseHibernate: insert into t_employee (empName, salary, dept_id) values (?, ?, ?)Hibernate: insert into t_employee (empName, salary, dept_id) values (?, ?, ?)Hibernate: insert into t_dept (deptName) values (?)Hibernate: update t_employee set deptId=? where empId=? 维护员工引用的部门的idHibernate: update t_employee set deptId=? where empId=?结果* inverse = trueHibernate: insert into t_employee (empName, salary, dept_id) values (?, ?, ?)Hibernate: insert into t_employee (empName, salary, dept_id) values (?, ?, ?)Hibernate: insert into t_dept (deptName) values (?)*/}// 【推荐】 保存, 部员方 【多的一方法操作】@Testpublic void save2() {Session session = sf.openSession();session.beginTransaction();// 部门对象Dept dept = new Dept();dept.setDeptName("综合部");// 员工对象Employee emp_zs = new Employee();emp_zs.setEmpName("张三");Employee emp_ls = new Employee();emp_ls.setEmpName("李四");// 关系emp_zs.setDept(dept);emp_ls.setDept(dept);// 保存session.save(dept); // 先保存一的方法session.save(emp_zs);session.save(emp_ls);// 再保存多的一方,关系回自动维护(映射配置完)session.getTransaction().commit();session.close();/*结果:无论inverse=true还是falseHibernate: insert into t_dept (deptName) values (?)Hibernate: insert into t_employee (empName, salary, dept_id) values (?, ?, ?)Hibernate: insert into t_employee (empName, salary, dept_id) values (?, ?, ?)*/}
}
----public void save() {...}执行分析
一方Dept.hbm.xml的Set集合inverse = true
Hibernate: insert into t_employee (empName, salary, dept_id) values (?, ?, ?)
Hibernate: insert into t_employee (empName, salary, dept_id) values (?, ?, ?)
Hibernate: insert into t_dept (deptName) values (?)
一方Dept.hbm.xml的Set集合Inverse=false
Hibernate: insert into t_employee (empName, salary, dept_id) values (?, ?, ?)
Hibernate: insert into t_employee (empName, salary, dept_id) values (?, ?, ?)
Hibernate: insert into t_dept (deptName) values (?)
Hibernate: update t_employee set dept_id=? where empId=?//deptId值有更新,和inverse=false有关,具有控制权
Hibernate: update t_employee set dept_id=? where empId=?//deptId值有更新
public void save2() {...}执行结果分析
一方Dept.hbm.xml的Set集合inverse = false
Hibernate: insert into t_dept (deptName) values (?)
Hibernate: insert into t_employee (empName, salary, dept_id) values (?, ?, ?)
Hibernate: insert into t_employee (empName, salary, dept_id) values (?, ?, ?)
-----、Inverse是hibernate双向关系中的基本概念。inverse的真正作用就是指定由哪一方来维护之间的关联关系。当一方中指定了“inverse=false”(默认),那么那一
方(主控方)就有责任负责之间的关联关系,说白了就是hibernate如何生成Sql来维护关联的记录!
Hibernate仅仅按照主控方对象的状态的变化来同步更新数据库。按照原来的映射文件,dept.getEmps().add(emp_zs);即主控方对象的状态发生了改变,因此数据库
会跟着对象状态的变化来同步更新数据库;而emp_zs.setDept(dept);,即被控方对象的状态发生了改变,它是不能触发对象和数据库的同步更新的。
这篇关于hibernate进阶之一对多和inverse属性的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!