Hibernate 关联映射

1.建立多对一的单向关联关系

01.配置单向多对一关联

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.happy.entity">
        <class name="Dept" table="DEPT">
        <id name="deptNo">
        <generator class="sequence">
        <param name="sqeuence">SEQ_NUM</param>
        </generator>
        </id>
        <set name="emps">
        <key column="DEPTNO"></key>
        <one-to-many class="Emp"></one-to-many>
        </set>
        <property name="deptName">  
        </property>
        </class>
        </hibernate-mapping>

 

在以上映射代码中,dept属性使Dept类型,而EMP表的外键DEPTNO是整数类型,显然类型不匹配,因此不能使用<property>元素来映射dept属性,而要使用<many-to-one>元素;

<many-to-one>元素建立了dept属性和EMP表的DEPTNO之间的映射

Emp.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.happy.entity">
        <class name="Emp" table="EMP">
        <id name="empid">
        <generator class="sequence">
        <param name="sqeuence">SEQ_NUM</param>
        </generator>
        </id>
        <property name="empname" type="string"></property>
        <many-to-one name="dept" column="deptNo" class="Dept"></many-to-one>
        </class>
        </hibernate-mapping>

03.测试类

    @Test
    public void test(){
        Session session = HibernateUtil.currentSession();
        Transaction tx = session.beginTransaction();//开始一个事务
        Dept dept=new Dept();//部门
        dept.setDeptName("小卖部");
        session.save(dept);//保存部门对象
        Emp emp=new Emp();//员工
        emp.setEmpname("李狗蛋");
        session.save(emp);
        tx.commit();//关闭事务
        HibernateUtil.closeSession();
    }

工具类

@Entity
public class HibernateUtil {
    private static final ThreadLocal<Session> sessionTL=new ThreadLocal<Session>();
    private static Configuration configuration;
    private final static SessionFactory sessionFactory;
    static{
        try {
            configuration=new Configuration().configure();
            sessionFactory=configuration.buildSessionFactory();
        } catch (Exception e) {
            e.printStackTrace();
            throw new ExceptionInInitializerError(e);
        }
        
    }
    public static Session currentSession(){
        Session session=sessionTL.get();
        //如果session为null,则打开一个新的session
        if (session==null) {
            session=sessionFactory.openSession();
            sessionTL.set(session);
        }
        return session;
    }
    public static void closeSession(){
            Session session=sessionTL.get();
            sessionTL.set(null);
            session.close();
    }
}

2.建立一对多双向关联关系

配置双向一对多关联

private Set<Emp> emps=new HashSet<Emp>();
    public Set<Emp> getEmps() {
        return emps;
    }
    public void setEmps(Set<Emp> emps) {
        this.emps = emps;
    }

在Dept.hbm.xml中写入<set>标签

 <set name="emps">
        <key column="DEPTNO"></key>
        <one-to-many class="Emp"></one-to-many>
        </set>

<set>name属性:设定持久化的属性名

<key>:column属性设定与所关联的持久化类对应的表的外键

<one-to-many>:class属性设定与所关联的持久化类

01.测试类

@Test
    public void onetomanyTest(){
        Session session = HibernateUtil.currentSession();
        Transaction tx = session.beginTransaction();
        Dept dept = (Dept)session.load(Dept.class, 1);
        //建立dept对象和emp对象的一对多双向关联关系
        Set<Emp> emps = dept.getEmps();
        for (Emp emp : emps) {
            System.out.println(emp.getEmpname());
        }
        //session.save(dept);//保存dept对象
        tx.commit();//关闭事务
        HibernateUtil.closeSession();
    }
@Test
    public void onetomanyTest(){
        Session session = HibernateUtil.currentSession();
        Transaction tx = session.beginTransaction();
        //建立dept对象和emp对象的一对多双向关联关系
        Dept dept=new Dept();
        Emp emp=new Emp();
        dept.setDeptName("中部");
        emp.setEmpname("张三");
        emp.setDept(dept);
        dept.getEmps().add(emp);
        //session.save(dept);//保存dept对象
        tx.commit();//关闭事务
        HibernateUtil.closeSession();
    }

先创建一个Dept对象和Emp对象,接着建立两者的一对多双向关联关系,最后调用sessiom.save(dept)方法持久化Dept对象。

posted @ 2016-08-12 16:46  菜鸟诶!  阅读(384)  评论(1编辑  收藏  举报