hibernate inverse=true

作者: 野鹤闲人
发布时间:2015-08-07 10:42:30

***

1,部门与员工是一对多,

部门或员工任何一方去维护他们的映射关系都可以,在关系表中都可以互相找到,可是在对象关系中,双方都要去维护,

如emp.setDepart(depart);depart.setEmps(emps);

这样hibernate会产生重复的更新语句,效率低,为了让对象之间的关系都维护好,而sql语句有没有多余,

通常让一的那一方不去维护之间的关联关系,就是用inverse=true;

解释为是否放弃维护关系--是,我的理解是:是否让对方去维护--是,因为inverse是倒转的,相反的意思

Department.java,

package com.maple.hibernate.domain;

import java.util.List;
import java.util.Map;
import java.util.Set;

public class Department {
    private int id;
    private String name;
    private Set<Employee> emps;
    //private Map<String,Employee> emps;
    
    /*private List<Employee> emps;*/
    
    public Set<Employee> getEmps() {
        return emps;
    }
    public void setEmps(Set<Employee> emps) {
        this.emps = emps;
    }
    /*public Map<String, Employee> getEmps() {
        return emps;
    }
    public void setEmps(Map<String, Employee> emps) {
        this.emps = emps;
    }*/
    /*public List<Employee> getEmps() {
        return emps;
    }
    public void setEmps(List<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.java

package com.maple.hibernate.domain;

public class Employee {
    private int id;
    private String name;
    private Department depart;
    
    public Department getDepart() {
        return depart;
    }
    public void setDepart(Department depart) {
        this.depart = depart;
    }
    @Override
    public String toString() {
        return "Employee [id=" + id + ", name=" + name + "]";
    }
    //private Department depart;
    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.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">
<hibernate-mapping package="com.maple.hibernate.domain">

    <class name="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>
        <!-- <list name="emps">
            <key column="depart_id"/>
            <list-index column="order_col"/>
            <one-to-many />
        </list> -->
        <!-- <bag name="emps">
            <key column="depart_id"/>
            <one-to-many />
        </bag> -->
        <!-- <map name="emps">
            <key column="depart_id"/>
            <map-key type="string" column="name"/>
            <one-to-many />
        </map> -->
    </class>

</hibernate-mapping>

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">
<hibernate-mapping package="com.maple.hibernate.domain">

    <class name="Employee"> 
        <id name="id">
            <generator class="native" /> 
        </id>
        <property name="name" unique="true"/>
        <many-to-one name="depart" column="depart_id"/>
    </class>

</hibernate-mapping>

测试

public class Many2One {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        add();
        queryDepart(1);
        //System.out.println("ff");
        //Employee emp=query(1);
        //System.out.println("ffff  depart name:"+emp.getDepart().getName());
    }

static Department add(){
        Session s=null;
        Transaction tx=null;
        try{
            s=HibernateUtil.getSession();
            /*List<Employee> emps=new ArrayList<Employee>();*/
            //Map<String,Employee> emps=new HashMap<String,Employee>();
            Set<Employee> emps=new HashSet<Employee>();
            Department depart=new Department();
            depart.setName("software");
            
            Employee emp=new Employee();
            emp.setDepart(depart); //导致更新语句
            emp.setName("aaa");
            
            Employee emp1=new Employee();
            emp1.setDepart(depart);  //导致更新语句
            emp1.setName("aaa1");
           
            /*emps.put(emp1.getName(), emp1);
            emps.put(emp.getName(), emp);  */
            emps.add(emp);
            emps.add(emp1);
            
            depart.setEmps(emps); //导致对employee表中depart_id的更新语句
            
            tx=s.beginTransaction();
            s.save(depart); //如果放下面,employee会多出一条更新语句
            s.save(emp); //如果设置了not-null=true,则出错
            s.save(emp1);
            tx.commit();
            System.out.println(depart.getEmps().getClass());
            //HashSet hs=(HashSet) depart.getEmps();//类转换异常
            
            return depart;
        }finally{
            if(s!=null){
                s.close();
            }
        }
    }
static Department queryDepart(int departid){
        Session s=null;
        
        try{
            s=HibernateUtil.getSession();
            Department depart=(Department) s.get(Department.class, departid);
            System.out.println("emps:"+depart.getEmps());
            return depart;
            
        }finally{
            if(s!=null){
                s.close();
            }
        }
    }

 还有些需要注意:

不要在有序集合如list,array中使用inverse, 

<!-- <list name="emps">
<key column="depart_id"/>
<list-index column="order_col"/>
<one-to-many />
</list> -->

你想,他都不去维护他们的映射关系了,又怎会关心他们添加的顺序

***

标签: Hibernate
来源:http://www.cnblogs.com/qingmaple/p/4116641.html

推荐: