inverse经常用在双向的1—N关联之中,也可以用在N—N的关联,这里举例子用双向的1—N关联
还是用部门和员工两个,关于两个的javabean不再写。
首先,我们看下员工的映射文件:
<?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="cn.itcast.hibernate.domain"> <class name="Employee" table="tb_Employee"> <id name="id"> <generator class="native"/> </id> <property name="name" /> <many-to-one name="depart" column="depart_id" /> </class> </hibernate-mapping>
<?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="cn.itcast.hibernate.domain"> <class name="Department" table="tb_Department"> <id name="id"> <generator class="native"/> </id> <property name="name" /> <set name="emps"> <key column="depart_id" /> <one-to-many class="Employee" /> </set> </class> </hibernate-mapping>
package cn.itcast.hibernate; import java.util.HashSet; import java.util.Set; import org.hibernate.Hibernate; import org.hibernate.Session; import org.hibernate.Transaction; import cn.itcast.hibernate.domain.Department; import cn.itcast.hibernate.domain.Employee; public class Many2One { public static void main(String[] arg){ Department depart = add(); } static Department add(){ Session s = null; Transaction tx = null; try{ Department depart = new Department(); Department depart1 = new Department(); depart.setName("depart name"); Employee emp1 = new Employee(); emp1.setDepart(depart); //建立两个对象的关联关系 emp1.setName("emp name"); Employee emp2 = new Employee(); emp2.setDepart(depart); //建立两个对象的关联关系 emp2.setName("emp name"); Set<Employee> emps = new HashSet<Employee>(); emps.add(emp1); emps.add(emp2); depart.setEmps(emps); s = HibernateUtil.getSession(); tx = s.beginTransaction(); s.save(depart); s.save(emp1); s.save(emp2); System.out.println(depart.getEmps().size()); tx.commit(); return depart; }finally{ if(s!=null){ s.close(); } } } }
我们看一下MyEclipse打印出来的sql语句:
Hibernate: insert into tb_Department (name) values (?) Hibernate: insert into tb_Employee (name, depart_id) values (?, ?) Hibernate: insert into tb_Employee (name, depart_id) values (?, ?) 2 Hibernate: update tb_Employee set depart_id=? where id=? Hibernate: update tb_Employee set depart_id=? where id=?
当双向1—N关联的时候,1的一端想要维护关系,N的一端也想要维护关系,在1的一端先插入,N的一端后插入之后,还要为N的一端外键再更新成为1的一端的id。所以会多出两条update语句。
但是,如果我们在部门映射文件中的set标签加入inverse="true"的属性后,这两条update语句就会消失,我们修改下部门映射文件:
<?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="cn.itcast.hibernate.domain"> <class name="Department" table="tb_Department"> <id name="id"> <generator class="native"/> </id> <property name="name" /> <set name="emps" inverse="true" > <key column="depart_id" /> <one-to-many class="Employee" /> </set> </class> </hibernate-mapping>
Hibernate: insert into tb_Department (name) values (?) Hibernate: insert into tb_Employee (name, depart_id) values (?, ?) Hibernate: insert into tb_Employee (name, depart_id) values (?, ?) 2
这是因为1的一端不再控制关联关系。
inverse=“true”表明1的一端不再控制关联关系
【SSH三大框架】Hibernate基础第十篇:inverse属性详解
原文地址:http://blog.csdn.net/u010800530/article/details/41244319