关联映射

 

关联映射

多对一(Employee -Department)

映射文件<many-to-one name=”depart” column=”depart_id”/>

 

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 {

 

         /**

          * @param args

          */

         public static void main(String[] args) {

                   add();

                   //query(1);

                   Employee el=query(1);

                   System.out.println(el.getName()+" "+el.getDepart().getName());

         }

         static Department add(){

                   Session s=null;

                   Transaction tx=null;

                   try{

                            s=HibernateUtil.getSession();

                            tx=s.beginTransaction();

                            //增加

                            Department dep=new Department();

                            dep.setName("depart one");

                            Employee el=new Employee();

                            el.setName("Tom");

                            el.setDepart(dep);//对象模型:对象建立关联关系

                            s.save(dep);

                            s.save(el);

                            tx.commit();

                            return dep;

                   }finally{

                            if(s!=null)

                                     s.close();

                   }

         }

         static Employee query(int empId){

                   Session s=null;

                   Transaction tx=null;

                   try{

                   s=HibernateUtil.getSession();

                   tx=s.beginTransaction();

                   //查询

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

                   //System.out.println(e.getName()+" "+e.getDepart().getName());

                   Hibernate.initialize(e.getDepart());

                   tx.commit();

                   return e;

         }finally{

                   if(s!=null)

                            s.close();

         }

         }

}

 

 

一对多(Department-Employee)

<set name=”employees”>

                   <key column=”depart_id”/>

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

</set>

 

package com.hbsi.test;

 

import java.util.HashSet;

import java.util.Set;

 

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 One2Many {

 

         /**

          * @param args

          */

         public static void main(String[] args) {

                   // TODO Auto-generated method stub

                   add();

                   //queryDepartment(8);

         }

         static Department add(){

                   Session s=null;

                   Transaction tx=null;

                   try{

                            s=HibernateUtil.getSession();

                            tx=s.beginTransaction();

                            //增加

                            Department dep=new Department();

                            dep.setName("depart one");

                            Employee el=new Employee();

                            el.setName("Tom");

                            el.setDepart(dep);//对象模型:对象建立关联关系,告诉员工你是哪个部门的

                           

                            Employee e2=new Employee();

                            e2.setName("yy");

                            e2.setDepart(dep);//对象模型:对象建立关联关系

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

                            set.add(el);

                            set.add(e2);

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

                            s.save(dep);

                            s.save(el);

                            s.save(e2);

                            tx.commit();

                            return dep;

                   }finally{

                            if(s!=null)

                                     s.close();

                   }

         }

         static Department queryDepartment(int depId){

                   Session s=null;

                   Transaction tx=null;

                   try{

                   s=HibernateUtil.getSession();

                   tx=s.beginTransaction();

                   //查询

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

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

                   tx.commit();

                   return d;

         }finally{

                   if(s!=null)

                            s.close();

         }

         }

}

一对一(Person-IdCard)

基于主键的one-to-one(person的映射文件)

<id name=”id”>

         <generator class=”foreign”><param name=”property”>idCard</param></generator>

<id>

<one-to-one name=”idCard” constrained=”true”/>

 

package com.hbsi.test;

 

import java.util.Date;

 

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.domain.IdCard;

import com.hbsi.domain.Person;

import com.hbsi.hibernate.utils.HibernateUtil;

 

public class One2One {

 

         /**

          * @param args

          */

         public static void main(String[] args) {

                   // TODO Auto-generated method stub

                   add();

                   query(2);

         }

         static Person add(){

                   Session s=null;

                   Transaction tx=null;

                   try{

                            s=HibernateUtil.getSession();

                            tx=s.beginTransaction();

                            //增加

                            IdCard idCard=new IdCard();

                            idCard.setUsefulLife(new Date());

                           

                            Person p=new Person();

                            p.setName("Rose");

                           

                            p.setIdCard(idCard);

                            idCard.setPerson(p);

                            s.save(p);

                            s.save(idCard);

                            tx.commit();

                            return p;

                   }finally{

                            if(s!=null)

                                     s.close();

                   }

         }

         static Person query(int personId){

                   Session s=null;

                   Transaction tx=null;

                   try{

                   s=HibernateUtil.getSession();

                   tx=s.beginTransaction();

                   //查询

                   Person p=(Person) s.get(Person.class, personId);

                   System.out.println(p.getName()+"   "+p.getIdCard().getUsefulLife());

                   tx.commit();

                   return p;

         }finally{

                   if(s!=null)

                            s.close();

         }

         }

}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值