hibernate集合映射,one-to-many,删除集合成员

本文详细介绍了在Hibernate中如何处理one-to-many集合映射,包括映射文件配置、模型类定义以及使用Hibernate工具类进行增删改查操作。通过JUnit测试代码展示了如何删除集合中的成员并更新实体。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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();
  }
 }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值