hibernate 多对一

**********

 1,部门和员工:多对一

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" />
        
    </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" />
        <many-to-one name="depart" column="depart_id"/>
    </class>

</hibernate-mapping>

 

hibernate.cfg.xml

<!DOCTYPE hibernate-configuration PUBLIC
    "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
    "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
    <session-factory>
        <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/test</property>
        <property name="hibernate.connection.username">root </property>
        <property name="hibernate.connection.password">root </property>
        
        <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
        <property name="hibernate.hbm2ddl.auto">create</property>
        <property name="show_sql">true</property>
       
        <mapping resource="com/maple/hibernate/domain/Department.hbm.xml"/>
        <mapping resource="com/maple/hibernate/domain/Employee.hbm.xml"/>
        
    </session-factory>
</hibernate-configuration>

 

Department.java

package com.maple.hibernate.domain;

public class Department {
    private int id;
    private String name;
    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 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;
    }
    
}

 

测试代码

package com.maple.hibernate;

import org.hibernate.Hibernate;
import org.hibernate.Session;
import org.hibernate.Transaction;

import com.maple.hibernate.domain.Department;
import com.maple.hibernate.domain.Employee;

public class Many2One {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        add();
        //System.out.println("ff");
        Employee emp=query(1);
        System.out.println("ffff  depart name:"+emp.getDepart().getName());//如果在query方法中没有
    }
    
    static Employee query(int employeeid){
        Session s=null;
        
        try{
            s=HibernateUtil.getSession();
            Employee emp=(Employee) s.get(Employee.class, employeeid);
            Hibernate.initialize(emp.getDepart());//若无,main中打印语句出错
//            System.out.println("depart name:"+emp.getDepart().getName());//可以
            return emp;
        }finally{
            if(s!=null){
                s.close();
            }
        }
    }
    
    static Department add(){
        Session s=null;
        Transaction tx=null;
        try{
            s=HibernateUtil.getSession();
            Department depart=new Department();
            depart.setName("software");
            
            Employee emp=new Employee();
            emp.setDepart(depart);
            emp.setName("aaa");
            
            tx=s.beginTransaction();
            s.save(depart); //如果放下面,employee会多出一条更新语句
            s.save(emp);
            tx.commit();
            return depart;
        }finally{
            if(s!=null){
                s.close();
            }
        }
    }

}

 

**********

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。