Hibernate中表与表之间的关系

 

1:多对1的关系

       例如部门Department和员工Employee的关系,

       一个部门可以有多个员工但是一个员工只能对应一个部门;

1:倒如相应的文件驱动;

       2:建立相应的Department类和Employee类

3; 建立相应的映射文件  Department 是1 的一方所以他的映射文件就是普通方式

的建立;Employee是多的一方所以又给他添加了一个属性  就是Department对象的

Depart、属性   在写属性的映射文件时:<many-to-one name=”depart” column=-“depart”/>

4:建立相应的工具包并建立相应的HibernateUtil类实现获取session对象

static{

    //’获取配置对象

       Configurationcfg=new Configuration();

       cfg.configure();//初始化

       //获取session工厂

       sessionFactory=cfg.buildSessionFactory();

    }

    public static SessionFactorygetSessionFactory(){

       return sessionFactory;

    }

    public static SessiongetSession(){

       //启动session

       return sessionFactory.openSession();

5:建立相应的测试类

    package com.hbsi.test;

 

import org.hibernate.Session;

import org.hibernate.Transaction;

 

import com.hbsi.domain.Department;

import com.hbsi.domain.Employ;

import com.hbsi.hibernate.utils.HibernateUtil;

 

public class Many2One {

 

    /**

     * @param args

     */

    public static void main(String[]args) {

       // TODO Auto-generated methodstub

       add();

       query(1);

      

    }

    //添加

    static Department add(){

       Session s=null;

       Transaction tx=null;

       try{

           s=HibernateUtil.getSession();

           tx=s.beginTransaction();

          

           //增加

           Department dep=newDepartment();

           dep.setName("departone");

          

          

           Employ el=new Employ();

           el.setName("tom");

           el.setDepart(dep);//对象建立关联

          

          

           s.save(dep);

           s.save(el);

          

          

           tx.commit();

           return dep;

       }finally{

           if(s!=null)

              s.close();

       }

      

    }

    //查询

    static Employ query(int empIy){

       Session s=null;

       Transaction tx=null;

       try{

           s=HibernateUtil.getSession();

           tx=s.beginTransaction();

          

           //查询

           Employ e=(Employ)s.get(Employ.class,empIy);

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

           tx.commit();

           return e;

       }finally{

           if(s!=null)

              s.close();

       }

    }

   

   

   

}

2:一对多的关系

    例如部门Department和员工Employee的关系,

       分析:一个部门可以有多个员工但是一个员工只能对应一个部门;

1:倒如相应的文件驱动;

       2:建立相应的Department类和Employee类

3; 建立相应的映射文件  Employee是1 的一方所以他的映射文件就是普通方式

的建立;Department是多的一方所以又给他添加了一个属性Employee对象的set集合名字是个empt  在写属性的该映射文件时:

<set name=”empt”>

<key name=”empt_id”/>

<one-to-many class=”Employee”>

</set>

4:建立相应的工具包并建立相应的HibernateUtil类实现获取session对象

static{

    //’获取配置对象

       Configurationcfg=new Configuration();

       cfg.configure();//初始化

       //获取session工厂

       sessionFactory=cfg.buildSessionFactory();

    }

    public static SessionFactorygetSessionFactory(){

       return sessionFactory;

    }

    public static SessiongetSession(){

       //启动session

       return sessionFactory.openSession();

5:建立相应的测试类

 

              package com.hbsi.test;

 

import java.util.HashSet;

import java.util.Set;

 

import org.hibernate.Session;

import org.hibernate.Transaction;

 

import com.hbsi.domain.Department;

import com.hbsi.domain.Employ;

importcom.hbsi.hibernate.utils.HibernateUtil;

 

public class One2Many {

       publicstatic void main(String[] args){

              add();

              queryDepartment(1);

       }

       //增加

       staticDepartment add(){

              Sessions=null;

              Transactiontx=null;

              try{

                     s=HibernateUtil.getSession();

                     tx=s.beginTransaction();

                    

                     //增加

                     Departmentdep=new Department();

                     dep.setName("departone");

                    

                    

                     Employel=new Employ();

                     el.setName("Tom");

                     el.setDepart(dep);//对象建立关联

                    

                     Employe2=new Employ();

                     e2.setName("jerry");

                     e2.setDepart(dep);

                    

                    

                    

                     Set<Employ>set=new HashSet<Employ>();

                     set.add(el);

                     set.add(e2);

                     dep.setEmps(set);//告诉部门有哪些员工

                    

                     s.save(dep);

                     s.save(el);

                     s.save(e2);

                    

                     tx.commit();

                     returndep;

              }finally{

                     if(s!=null)

                            s.close();

              }

             

       }

      

      

       //查询

              staticDepartment queryDepartment(int depId){

                     Sessions=null;

                     Transactiontx=null;

                     try{

                            s=HibernateUtil.getSession();

                            tx=s.beginTransaction();

                           

                            //查询

                            Departmentd=(Department) s.get(Department.class,depId);

                            System.out.println(d.getName()+"有"+d.getEmps().size()+"个员工");

                           

                            tx.commit();

                            returnd;

                     }finally{

                            if(s!=null)

                                   s.close();

                     }

              }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值