码迷,mamicode.com
首页 > Web开发 > 详细

hibernate 多对一

时间:2014-11-16 18:36:11      阅读:305      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   io   color   ar   os   sp   java   

**********

 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();
            }
        }
    }

}

 

**********

hibernate 多对一

标签:style   blog   http   io   color   ar   os   sp   java   

原文地址:http://www.cnblogs.com/qingmaple/p/4101690.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!