知识点:

【 


多对一(Employee - Department) 

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

column=”depart_id”与Employee外键对应 默认于Department主键对应 

也可以通过property-ref来指定引用那个属性与外键对应 


一对多(Department-Employee) 

//映射集合 

<set name=”employees”> 

//指定查询根据depart_id去Employee查询 

 <key column=”depart_id”/> 

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

</set> 


】


照样看例子:

第一步搭建hibernate环境和先关配置文件件hibernate环境搭建

第二步:编写Employee Department 实体类

Department.java: 


public class Department { 


 private int id; 

 private String name; 

 private Set<Employee> employees; 


 public int getId() { 

 return id; 

 } 


 public void setId(int id) { 

 this.id = id; 

 } 


 public String getName() { 

 return name; 

 } 


 public void setName(String name) { 

 this.name = name; 

 } 


 public Set<Employee> getEmployees() { 

 return employees; 

 } 


 public void setEmployees(Set<Employee> employees) { 

 this.employees = employees; 

 } 


} 


Employee.java: 


public class Employee { 


 private int id; 

 private String name; 

 private Department department; 


 public Department getDepartment() { 

 return department; 

 } 


 public void setDepartment(Department department) { 

 this.department = department; 

 } 


 public int getId() { 

 return id; 

 } 


 public void setId(int id) { 

 this.id = id; 

 } 


 public String getName() { 

 return name; 

 } 


 public void setName(String name) { 

 this.name = name; 

 } 


} 


第三步:编写类的映射文件 分别为:配置文件命名规则见上面博客 


Department.hbm.xml 


<?xml version="1.0"?> 

<!DOCTYPE hibernate-mapping PUBLIC 

 "-//Hibernate/Hibernate Mapping DTD 3.0//EN" 

 "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> 

<hibernate-mapping package="com.liyong.domain"> 


 <class name="Department"> 

 <!-- 生成主键 以natvie 自增长--> 

 <id name="id"> 

 <generator class="native" /> 

 </id> 

 <property name="name" /> 

 <set name="employees"> 

 <key column="department_id"/> 

 <one-to-many class="com.liyong.domain.Employee"/> 

 </set> 

 </class> 


</hibernate-mapping> 


Employee.hbm.xml 


<?xml version="1.0"?> 

<!DOCTYPE hibernate-mapping PUBLIC 

 "-//Hibernate/Hibernate Mapping DTD 3.0//EN" 

 "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> 

<hibernate-mapping package="com.liyong.domain"> 


 <class name="Employee"> 

 <!-- 生成主键 以natvie 自增长--> 

 <id name="id"> 

 <generator class="native" /> 

 </id> 

 <property name="name" /> 

 <!--与department属性进行映射column指定这个外键的名称 这里是默认情况下 

 通过反射机制得到department属性的类然后通过这个类的属性文件与department类主键 

 于这个Employee外键进行映射,也可以通过property-ref来指定于这个外键对应的映射主键 

 是department那个属性一般都不会使用这个property-ref="name"则这个表的外键是department类中name属性 

 --> 

 <many-to-one name="department" column="department_id" /> 

 </class> 


</hibernate-mapping> 


第四步:编写测试类 


public class Test { 


 /** 

 * @param args 

 */ 

 public static void main(String[] args) { 


 //ManyToOneAdd(); 


 //ManyToOnd 

 // Employee employee=ManyToOneQuery(1); 

 // //这里因为对代理对象进行了初始所有可以在这里得到引用对象 

 // System.out.println("name:"+employee.getDepartment().getName()); 



 //OneToMany 

 Department department=QueryDepartment(1); 

 System.out.println("department length:"+department.getEmployees().size()); 

 } 


 public static void ManyToOneAdd() 

 { 

 Session session=null; 

 Transaction transaction=null; 


 Department department=new Department(); 

 department.setName("学生部"); 


 Employee employee1=new Employee(); 

 employee1.setName("张三"); 

 employee1.setDepartment(department); 


 Employee employee=new Employee(); 

 employee.setName("李勇"); 

 employee.setDepartment(department); 

 try { 


 session = HibernateUtil.getSession(); 

 transaction=session.beginTransaction(); 


 session.save(department); 

 session.save(employee); 

 session.save(employee1); 


 transaction.commit(); 

 } catch (Exception e) { 


 if(session!=null) 

 { 

 session.close(); 

 } 

 } 

 } 

 //many-to-on 

 public static Employee QueryEmployee(int id) 

 { 

 Session session=null; 

 Transaction transaction=null; 

 Employee employee=null; 

 try { 


 session = HibernateUtil.getSession(); 

 transaction=session.beginTransaction(); 

 //这里得到是一个代理对象 

 employee=(Employee)session.get(Employee.class, id); 

 System.out.println(employee); 

 String name=employee.getDepartment().getName(); 

 //初始化代理对象 

 Hibernate.initialize(employee); 

 //System.out.println("name:"+name); 

 transaction.commit(); 

 } catch (Exception e) { 


 if(session!=null) 

 { 

 session.close(); 

 } 

 return null; 

 } 

 return employee; 

 } 

 //one-to-many 

 public static Department QueryDepartment(int id) 


 { 

 Session session=null; 

 Transaction transaction=null; 

 Department department=null; 

 try { 


 session = HibernateUtil.getSession(); 

 transaction=session.beginTransaction(); 


 //下面这种查询只能通过Id查询 

 department=(Department)session.get(Department.class, id); 


 Hibernate.initialize(department); 

 transaction.commit(); 

 } catch (Exception e) { 


 if(session!=null) 

 { 

 session.close(); 

 } 

 return null; 

 } 

 return department; 

 } 

} 


第五步:测试.... 


源码见附件: