【hibernate实例】双项一对多(多对一)增加删除


新建包www.hbsi.net.one2mantboth

新建持久化类
Department.java
  1. package www.hbsi.net.one2manyboth;  
  2.   
  3. import java.util.Set;  
  4. public class Department {  
  5.   
  6.     private int id;  
  7.     private String name;  
  8.     private Set<Employee> emps;  
  9.   
  10.     public int getId() {  
  11.         return id;  
  12.     }  
  13.   
  14.     public void setId(int id) {  
  15.         this.id = id;  
  16.     }  
  17.   
  18.     public String getName() {  
  19.         return name;  
  20.     }  
  21.   
  22.     public void setName(String name) {  
  23.         this.name = name;  
  24.     }  
  25.   
  26.     public Set<Employee> getEmps() {  
  27.         return emps;  
  28.     }  
  29.   
  30.     public void setEmps(Set<Employee> emps) {  
  31.         this.emps = emps;  
  32.     }  
  33.   
  34. }  


Employee.java
  1. package www.hbsi.net.one2manyboth;  
  2.   
  3. public class Employee {  
  4.     private int id;  
  5.     private String username;  
  6.     private Department dept;  
  7.   
  8.     public int getId() {  
  9.         return id;  
  10.     }  
  11.   
  12.     public void setId(int id) {  
  13.         this.id = id;  
  14.     }  
  15.   
  16.     public String getUsername() {  
  17.         return username;  
  18.     }  
  19.   
  20.     public void setUsername(String username) {  
  21.         this.username = username;  
  22.     }  
  23.   
  24.     public void setDept(Department dept) {  
  25.         this.dept = dept;  
  26.     }  
  27.   
  28.     public Department getDept() {  
  29.         return dept;  
  30.     }  
  31.   
  32. }  


新建持久化类配置文件
Department.hbm.xml
  1. <?xml version="1.0"?>  
  2. <!DOCTYPE hibernate-mapping PUBLIC  
  3.       "-//Hibernate/Hibernate Mapping DTD 3.0//EN"  
  4.           "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">  
  5. <hibernate-mapping package="www.hbsi.net.one2manyboth">  
  6.    <class name="Department" table="department">  
  7.       <id name="id" column="id">  
  8.         <generator class="native"/>  
  9.       </id>  
  10.       <property name="name" column="name" type="string"/>  
  11.        
  12.       <set name="emps" table="employee">  
  13.          <key column="dept_id"/>  
  14.          <one-to-many class="Employee"/>  
  15.       </set>  
  16.      
  17.    </class>  
  18. </hibernate-mapping>  


Employee.hbm.xml
  1. <?xml version="1.0"?>  
  2. <!DOCTYPE hibernate-mapping PUBLIC  
  3.       "-//Hibernate/Hibernate Mapping DTD 3.0//EN"  
  4.           "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">  
  5.   
  6. <hibernate-mapping package="www.hbsi.net.one2manyboth">  
  7.    <class name="Employee" table="employee">  
  8.       <id name="id" column="id">  
  9.         <generator class="native"/>  
  10.       </id>  
  11.       <property name="username" column="username" type="string"/>  
  12.       <many-to-one name="dept" column="dept_id"></many-to-one>  
  13.    </class>  
  14. </hibernate-mapping>  
更改hibernate配置文件

hibernate.cfg.xml

  1. <?xml version='1.0' encoding='UTF-8'?>  
  2. <!DOCTYPE hibernate-configuration PUBLIC  
  3.           "-//Hibernate/Hibernate Configuration DTD 3.0//EN"  
  4.           "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">  
  5.   
  6. <!-- Generated by MyEclipse Hibernate Tools.                   -->  
  7. <hibernate-configuration>  
  8.   
  9.     <session-factory>  
  10.         <property name="dialect">  
  11.             org.hibernate.dialect.MySQLDialect  
  12.         </property>  
  13.         <property name="connection.url">  
  14.             jdbc:mysql://localhost:3306/hibernate  
  15.         </property>  
  16.         <property name="connection.username">root</property>  
  17.         <property name="connection.password">123456</property>  
  18.         <property name="connection.driver_class">  
  19.             com.mysql.jdbc.Driver  
  20.         </property>  
  21.         <property name="show_sql">true</property>  
  22.         <property name="hbm2ddl.auto">update</property>  
  23.         <mapping resource="www/hbsi/net/one2manyboth/Department.hbm.xml" />  
  24.         <mapping resource="www/hbsi/net/one2manyboth/Employee.hbm.xml" />  
  25.   
  26.     </session-factory>  
  27.   
  28. </hibernate-configuration>  

增加
One2manyboth.java(1)一方放弃维护
invarse属性的意思是:你要放弃维护吗,设置成true,默认为false
Department.hbm.xml
  1. <?xml version='1.0' encoding='UTF-8'?>  
  2. <!DOCTYPE hibernate-configuration PUBLIC  
  3.           "-//Hibernate/Hibernate Configuration DTD 3.0//EN"  
  4.           "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">  
  5.   
  6. <!-- Generated by MyEclipse Hibernate Tools.                   -->  
  7. <hibernate-configuration>  
  8.   
  9.     <session-factory>  
  10.         <property name="dialect">  
  11.             org.hibernate.dialect.MySQLDialect  
  12.         </property>  
  13.         <property name="connection.url">  
  14.             jdbc:mysql://localhost:3306/hibernate  
  15.         </property>  
  16.         <property name="connection.username">root</property>  
  17.         <property name="connection.password">123456</property>  
  18.         <property name="connection.driver_class">  
  19.             com.mysql.jdbc.Driver  
  20.         </property>  
  21.         <property name="show_sql">true</property>  
  22.         <property name="hbm2ddl.auto">update</property>  
  23.         <mapping resource="www/hbsi/net/one2manyboth/Department.hbm.xml" />  
  24.         <mapping resource="www/hbsi/net/one2manyboth/Employee.hbm.xml" />  
  25.   
  26.     </session-factory>  
  27.   
  28. </hibernate-configuration>  

One2manyboth.java

  1. package www.hbsi.net.one2manyboth;  
  2.   
  3. import org.hibernate.Session;  
  4. import org.junit.Test;  
  5.   
  6. import www.hbsi.net.util.HibernateSessionFactory;  
  7.   
  8. public class One2manyboth {  
  9.   
  10.     @Test  
  11.     public void add() {  
  12.         Session session = HibernateSessionFactory.getSession();  
  13.         session.beginTransaction();  
  14.   
  15.         //创建部门  
  16.         Department dept = new Department();  
  17.         dept.setName("部门1");  
  18.   
  19.         //创建雇员1  
  20.         Employee emp1 = new Employee();  
  21.         emp1.setUsername("雇员1");  
  22.         //告诉hibernate部员是哪个部门  
  23.         emp1.setDept(dept);  
  24.         //创建雇员2  
  25.         Employee emp2 = new Employee();  
  26.         emp2.setUsername("雇员2");  
  27.         emp2.setDept(dept);  
  28.   
  29.       
  30.         session.save(emp1);  
  31.         session.save(emp2);  
  32.         session.save(dept);  
  33.           
  34.   
  35.         session.getTransaction().commit();  
  36.         HibernateSessionFactory.closeSession();  
  37.     }  
  38.   
  39. }  







One2manyboth.java(2)级联增加
Department.hbm.xml
  1. <?xml version="1.0"?>  
  2. <!DOCTYPE hibernate-mapping PUBLIC  
  3.       "-//Hibernate/Hibernate Mapping DTD 3.0//EN"  
  4.           "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">  
  5. <hibernate-mapping package="www.hbsi.net.one2manyboth">  
  6.    <class name="Department" table="department">  
  7.       <id name="id" column="id">  
  8.         <generator class="native"/>  
  9.       </id>  
  10.       <property name="name" column="name" type="string"/>  
  11.        
  12.       <set name="emps" table="employee" inverse="true">  
  13.          <key column="dept_id"/>  
  14.          <one-to-many class="Employee"/>  
  15.       </set>  
  16.      
  17.    </class>  
  18. </hibernate-mapping>  

Employee.hbm.xml
  1. <?xml version="1.0"?>  
  2. <!DOCTYPE hibernate-mapping PUBLIC  
  3.       "-//Hibernate/Hibernate Mapping DTD 3.0//EN"  
  4.           "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">  
  5.   
  6. <hibernate-mapping package="www.hbsi.net.one2manyboth">  
  7.    <class name="Employee" table="employee">  
  8.       <id name="id" column="id">  
  9.         <generator class="native"/>  
  10.       </id>  
  11.       <property name="username" column="username" type="string"/>  
  12.       <many-to-one name="dept" column="dept_id" cascade="all"></many-to-one>  
  13.    </class>  
  14. </hibernate-mapping>  

One2manyboth.java
  1. package www.hbsi.net.one2manyboth;  
  2.   
  3. import org.hibernate.Session;  
  4. import org.junit.Test;  
  5.   
  6. import www.hbsi.net.util.HibernateSessionFactory;  
  7.   
  8. public class One2manyboth {  
  9.   
  10.     @Test  
  11.     public void add() {  
  12.         Session session = HibernateSessionFactory.getSession();  
  13.         session.beginTransaction();  
  14.   
  15.         // 创建部门  
  16.         Department dept = new Department();  
  17.         dept.setName("部门1");  
  18.   
  19.         // 创建雇员1  
  20.         Employee emp1 = new Employee();  
  21.         emp1.setUsername("雇员1");  
  22.         // 告诉hibernate部员是哪个部门  
  23.         emp1.setDept(dept);  
  24.         // 创建雇员2  
  25.         Employee emp2 = new Employee();  
  26.         emp2.setUsername("雇员2");  
  27.         emp2.setDept(dept);  
  28.   
  29.         session.save(emp1);  
  30.         session.save(emp2);  
  31.       
  32.   
  33.         session.getTransaction().commit();  
  34.         HibernateSessionFactory.closeSession();  
  35.     }  
  36.   
  37. }  



One2manyboth.java(3)级联删除_删除雇员时删除部门
Department.hbm.xml
  1. <?xml version="1.0"?>  
  2. <!DOCTYPE hibernate-mapping PUBLIC  
  3.       "-//Hibernate/Hibernate Mapping DTD 3.0//EN"  
  4.           "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">  
  5. <hibernate-mapping package="www.hbsi.net.one2manyboth">  
  6.    <class name="Department" table="department">  
  7.       <id name="id" column="id">  
  8.         <generator class="native"/>  
  9.       </id>  
  10.       <property name="name" column="name" type="string"/>  
  11.        
  12.       <set name="emps" table="employee" cascade="save-update">  
  13.          <key column="dept_id"/>  
  14.          <one-to-many class="Employee"/>  
  15.       </set>  
  16.      
  17.    </class>  
  18. </hibernate-mapping>  

Employee.hbm.xml
  1. <?xml version="1.0"?>  
  2. <!DOCTYPE hibernate-mapping PUBLIC  
  3.       "-//Hibernate/Hibernate Mapping DTD 3.0//EN"  
  4.           "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">  
  5.   
  6. <hibernate-mapping package="www.hbsi.net.one2manyboth">  
  7.    <class name="Employee" table="employee">  
  8.       <id name="id" column="id">  
  9.         <generator class="native"/>  
  10.       </id>  
  11.       <property name="username" column="username" type="string"/>  
  12.       <many-to-one name="dept" column="dept_id" cascade="delete"></many-to-one>  
  13.    </class>  
  14. </hibernate-mapping>  

One2manyboth.java
  1. package www.hbsi.net.one2manyboth;  
  2.   
  3. import org.hibernate.Session;  
  4. import org.junit.Test;  
  5.   
  6. import www.hbsi.net.util.HibernateSessionFactory;  
  7.   
  8. public class One2manyboth {  
  9.   
  10.     @Test  
  11.     public void add() {  
  12.         Session session = HibernateSessionFactory.getSession();  
  13.         session.beginTransaction();  
  14.   
  15.         // 创建部门  
  16.         Department dept = new Department();  
  17.         dept.setName("部门1");  
  18.   
  19.         // 创建雇员1  
  20.         Employee emp1 = new Employee();  
  21.         emp1.setUsername("雇员1");  
  22.         // 告诉hibernate部员是哪个部门  
  23.         emp1.setDept(dept);  
  24.         // 创建雇员2  
  25.         Employee emp2 = new Employee();  
  26.         emp2.setUsername("雇员2");  
  27.         emp2.setDept(dept);  
  28.   
  29.         session.save(emp1);  
  30.         session.save(emp2);  
  31.       
  32.   
  33.         session.getTransaction().commit();  
  34.         HibernateSessionFactory.closeSession();  
  35.     }  
  36.     //级联删除  
  37.       
  38.     @Test  
  39.     public void delete(){  
  40.         Session session = HibernateSessionFactory.getSession();  
  41.         session.beginTransaction();  
  42.           
  43.         Employee emp = (Employee) session.get(Employee.class,45);  
  44.         session.delete(emp);  
  45.           
  46.         session.getTransaction().commit();  
  47.         HibernateSessionFactory.closeSession();  
  48.     }  
  49.   
  50. }  


删除前:


删除后:


One2manyboth.java(4)级联删除_删除部门时删除雇员

Department.hbm.xml
  1. <?xml version="1.0"?>  
  2. <!DOCTYPE hibernate-mapping PUBLIC  
  3.       "-//Hibernate/Hibernate Mapping DTD 3.0//EN"  
  4.           "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">  
  5. <hibernate-mapping package="www.hbsi.net.one2manyboth">  
  6.    <class name="Department" table="department">  
  7.       <id name="id" column="id">  
  8.         <generator class="native"/>  
  9.       </id>  
  10.       <property name="name" column="name" type="string"/>  
  11.        
  12.       <set name="emps" table="employee" cascade="save-update,delete">  
  13.          <key column="dept_id"/>  
  14.          <one-to-many class="Employee"/>  
  15.       </set>  
  16.      
  17.    </class>  
  18. </hibernate-mapping>  

Employee.hbm.xml
  1. <?xml version="1.0"?>  
  2. <!DOCTYPE hibernate-mapping PUBLIC  
  3.       "-//Hibernate/Hibernate Mapping DTD 3.0//EN"  
  4.           "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">  
  5.   
  6. <hibernate-mapping package="www.hbsi.net.one2manyboth">  
  7.    <class name="Employee" table="employee">  
  8.       <id name="id" column="id">  
  9.         <generator class="native"/>  
  10.       </id>  
  11.       <property name="username" column="username" type="string"/>  
  12.       <many-to-one name="dept" column="dept_id"></many-to-one>  
  13.    </class>  
  14. </hibernate-mapping>  

One2manyboth.java
  1. package www.hbsi.net.one2manyboth;  
  2.   
  3. import org.hibernate.Session;  
  4. import org.junit.Test;  
  5.   
  6. import www.hbsi.net.util.HibernateSessionFactory;  
  7.   
  8. public class One2manyboth {  
  9.   
  10.     @Test  
  11.     public void add() {  
  12.         Session session = HibernateSessionFactory.getSession();  
  13.         session.beginTransaction();  
  14.   
  15.         // 创建部门  
  16.         Department dept = new Department();  
  17.         dept.setName("部门1");  
  18.   
  19.         // 创建雇员1  
  20.         Employee emp1 = new Employee();  
  21.         emp1.setUsername("雇员1");  
  22.         // 告诉hibernate部员是哪个部门  
  23.         emp1.setDept(dept);  
  24.         // 创建雇员2  
  25.         Employee emp2 = new Employee();  
  26.         emp2.setUsername("雇员2");  
  27.         emp2.setDept(dept);  
  28.   
  29.         session.save(emp1);  
  30.         session.save(emp2);  
  31.       
  32.   
  33.         session.getTransaction().commit();  
  34.         HibernateSessionFactory.closeSession();  
  35.     }  
  36.     //级联删除  
  37.       
  38.     @Test  
  39.     public void delete(){  
  40.         Session session = HibernateSessionFactory.getSession();  
  41.         session.beginTransaction();  
  42.           
  43.         //Employee emp = (Employee) session.get(Employee.class,45);  
  44.         //session.delete(emp);  
  45.           
  46.         Department dept = (Department) session.get(Department.class,26);  
  47.         session.delete(dept);  
  48.           
  49.           
  50.         session.getTransaction().commit();  
  51.         HibernateSessionFactory.closeSession();  
  52.     }  
  53.   
  54. }  

删除前:


删除后:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值