1、one方映射文件
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!--
Mapping file autogenerated by MyEclipse Persistence Tools
-->
<hibernate-mapping>
<class name="com.epro.examsys.model.Department" table="t_department" catalog="edu">
<id name="id" type="java.lang.Integer">
<column name="id" />
<generator class="native" />
</id>
<property name="name" type="java.lang.String">
<column name="departName" not-null="true" />
</property>
<set name="emps" lazy="false" inverse="true" cascade="save-update, delete,delete-orphan">
<key>
<column name="depart_id"></column>
</key>
<one-to-many class="com.epro.examsys.model.Employee"/>
</set>
</class>
</hibernate-mapping>
2、many方映射文件
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!--
Mapping file autogenerated by MyEclipse Persistence Tools
-->
<hibernate-mapping>
<class name="com.epro.examsys.model.Employee" table="t_employee" catalog="edu">
<id name="id" type="java.lang.Integer">
<column name="id" />
<generator class="native" />
</id>
<property name="name" type="java.lang.String">
<column name="empName" not-null="true" />
</property>
<many-to-one name="depart" column="depart_id" class="com.epro.examsys.model.Department" lazy="false">
</many-to-one>
</class>
</hibernate-mapping>
3、one方model文件
package com.epro.examsys.model;
import java.util.Set;
public class Department {
private int id;
private Set<Employee> emps;
public Set<Employee> getEmps() {
return emps;
}
public void setEmps(Set<Employee> emps) {
this.emps = emps;
}
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;
}
private String name;
}
4、many方model文件
package com.epro.examsys.model;
public class Employee {
private int id;
private String name;
private Department depart;
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 Department getDepart() {
return depart;
}
public void setDepart(Department depart) {
this.depart = depart;
}
}
5、Hibernate工具类
package com.epro.examsys.util;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
public final class HibernateUtil {
private static SessionFactory sessionFactory;
private HibernateUtil(){
}
static
{
Configuration cfg = new Configuration();
cfg.configure();//加载classpath中的hibernate配置文件hibernate.cfg.xml
sessionFactory = cfg.buildSessionFactory();
}
public static SessionFactory getSessionFactory()
{
return sessionFactory;
}
public static Session getSession(){
return sessionFactory.openSession();
}
static void add(Object obj)
{
Session s = null;
Transaction tx = null;
try{
s = HibernateUtil.getSession();
tx = s.beginTransaction();
s.save(obj);
tx.commit();
}catch(HibernateException ex){
if(tx!=null)
tx.rollback();
ex.printStackTrace();
throw ex;
}finally{
if(null!=s)
s.close();
}
}
static void del(Object obj)
{
Session s = null;
Transaction tx = null;
try{
s = HibernateUtil.getSession();
tx = s.beginTransaction();
s.delete(obj);
tx.commit();
}catch(HibernateException ex){
if(tx!=null)
tx.rollback();
ex.printStackTrace();
throw ex;
}finally{
if(null!=s)
s.close();
}
}
static void update(Object obj)
{
Session s = null;
Transaction tx = null;
try{
s = HibernateUtil.getSession();
tx = s.beginTransaction();
s.update(obj);
tx.commit();
}catch(HibernateException ex){
if(tx!=null)
tx.rollback();
ex.printStackTrace();
throw ex;
}finally{
if(null!=s)
s.close();
}
}
}
6、junit测试代码
@Test
public void testSaveOrUpdateDepartment(){
Session s = null;
Transaction tx = null;
try
{
s = HibernateUtil.getSessionFactory().openSession();
tx = s.beginTransaction();
Department depart = (Department) s.get(Department.class, 2);
Employee e = depart.getEmps().iterator().next();
System.out.println("begin:"+depart.getEmps().size());
depart.getEmps().remove(e);
depart.setName("hahaha...");
System.out.println("end:"+depart.getEmps().size());
tx.commit();
Assert.assertTrue(true);
}catch(HibernateException hEX){
hEX.printStackTrace();
if(tx!=null)
tx.rollback();
Assert.assertFalse(false);
}
finally
{
if(s!=null)
s.close();
}
}