关联映射
多对一(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();
}
}
}