Hibernate关联关系

来源:互联网 发布:传智播客java视频 编辑:程序博客网 时间:2024/05/31 19:35
 

       Hibenrnate的关联关系是指不同持久类之间的一种结果关系,简单地说,关联关系描述某个对象在某一段时间内一直知道另一个对象的存在。

       关联关系包括多样性的关联和方向性的关联。多样性指的是一个持久化类的对象跟另一持久化类的多个对象关联(一对多),还是只能跟以另一个持久化类的一个对象关联(一对一),还是以就是双向都可以与另一方的多个对象关联(多对多);方向性指的是关联的双发是否可以互相访问(双向关联),还是只有一端可以访问另一端(单向关联)。

1、单项多对一:

       多对一是最常见,最容易理解的一种关联;

       示例:多个员工属于同一个部门;

Department(部门)    Employee(员工) 

 

搭建好Hibernate,建domain,util,test包;

Hibernate.cfg.xml配置文件:

<!DOCTYPE hibernate-configuration PUBLIC

   "-//Hibernate/Hibernate Configuration DTD 3.0//EN"

   "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">

<!-- 配置文件   驱动 url 用户名  密码  方言-->

<hibernate-configuration>

   <session-factory>

     <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>

     <property name="hibernate.connection.url">jdbc:mysql:///demo</property>

     <property name="hibernate.connection.username">root</property>

     <property name="hibernate.connection.password">042181</property>

     <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>

     <property name="hibernate.hbm2ddl.auto">create</property>

     <property name="hibernate.show_sql">true</property>

     <mapping resource="com/hbsi/domain/Department.hbm.xml"/><!-- 指定映射文件的位置 -->

     <mapping resource="com/hbsi/domain/Employee.hbm.xml"/><!-- 指定映射文件的位置 -->

   </session-factory>

</hibernate-configuration>

Domain包:

1)、Department类

  package com.hbsi.domain;

 

import java.util.Set;

 

public class Department {

   private int id;

   /* 部门名称*/

   private String name;

   private Set<Employee> emps;

   /*自己加上所有属性的geters和setters方法*/

}

2)、Deparment.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="com.hbsi.domain">

   <class name="Department" table="department">

     <id name="id" column="id">

        <generator class="native"/><!-- native主键的生成器自动增长 -->

     </id>

     <property name="name" column="name"  not-null="true"/><!-- 映射普通的java属性 -->

     <set name="emps">

        <key column="depart_id"/>

        <one-to-many class="Employee"/>

     </set>

   </class>

</hibernate-mapping>

3)、Employeee类

package com.hbsi.domain;

 

public class Employee {

  

   private int id;

   /*员工名称*/

   private String name;

   /*特定部门*/

   private Department depart;

   /*自己加上所有属性的geters和setters方法*/

}

4)Employee.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="com.hbsi.domain">

   <class name="Employee" table="employee">

     <id name="id" column="id" >

        <generator class="native"/><!-- native主键的生成器自动增长 -->

     </id>

     <property name="name" column="name"  not-null="true"/><!-- 映射普通的java属性 -->

     <many-to-one name="depart" column="depart_Id" not-null="true" />

   </class>

</hibernate-mapping>

Uitl包:

1)、HibernateUitl类

package com.hbsi.hibernate.utils;

import org.hibernate.Session;

import org.hibernate.SessionFactory;

import org.hibernate.cfg.Configuration;

public final class HibernateUtil {

       private static SessionFactory sessionFactory;

       private HibernateUtil(){    

       }

       static{

              //使用Hibernate完成将对象存入表中。

              Configuration cfg=new Configuration();

              //用来完成Hibernate的初始化--读取配置文件

              cfg.configure();

              //sessionFactory对象---DriverManager

              sessionFactory=cfg.buildSessionFactory();

       }

       public static SessionFactory getSessionFactory() {

              return sessionFactory;

       }

       public static Session getSession(){

              return sessionFactory.openSession();

       }

}

Test包:

Many2One类:

package com.hbsi.test;

import org.hibernate.Hibernate;

import org.hibernate.Session;

import org.hibernate.Transaction;

import com.hbsi.domain.Department;

import com.hbsi.domain.Employee;

import com.hbsi.hibernate.utils.HibernateUtil;

public class Many2One {

      

       public static void main(String[] args) {

              add();

              Employee e=query(1);

              System.out.println(e.toString());

       }

       /*添加对象方法*/

       static Department add(){

              Session session=null;

              Transaction transaction=null;

              try{

                     session=HibernateUtil.getSession();

                     transaction=session.beginTransaction();

                     Department dep=new Department();

                     dep.setName("depart one");

                     Employee e1=new Employee();

                     e1.setName("Tom");

                     /*对象模型,对象关联*/

                     e1.setDepart(dep);

                     session.save(dep);

                     session.save(e1);

                     transaction.commit();

                     return dep;

              }finally{

                     if(session!=null){

                            session.close();

                     }

              }

       }

       static Employee query(int empId){

              Session session=null;

              Transaction transaction=null;

              try{

                     session=HibernateUtil.getSession();

                     transaction=session.beginTransaction();

                     Employee e=(Employee) session.get(Employee.class,empId);

                     System.out.println(e.toString());

                     Hibernate.initialize(e.getDepart());

                     transaction.commit();

                     return e;

              }finally{

                     if(session!=null){

                            session.close();

                     }

              }     

       }

}

 

原创粉丝点击