新建包www.hbsi.net.one2mantboth
![](https://img-my.csdn.net/uploads/201301/05/1357356781_8144.jpg)
新建持久化类
Department.java
package www.hbsi.net.one2manyboth;
import java.util.Set;
public class Department {
private int id;
private String name;
private Set<Employee> 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;
}
public Set<Employee> getEmps() {
return emps;
}
public void setEmps(Set<Employee> emps) {
this.emps = emps;
}
}
Employee.java
package www.hbsi.net.one2manyboth;
public class Employee {
private int id;
private String username;
private Department dept;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public void setDept(Department dept) {
this.dept = dept;
}
public Department getDept() {
return dept;
}
}
新建持久化类配置文件
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="www.hbsi.net.one2manyboth">
<class name="Department" table="department">
<id name="id" column="id">
<generator class="native"/>
</id>
<property name="name" column="name" type="string"/>
<set name="emps" table="employee">
<key column="dept_id"/>
<one-to-many class="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="www.hbsi.net.one2manyboth">
<class name="Employee" table="employee">
<id name="id" column="id">
<generator class="native"/>
</id>
<property name="username" column="username" type="string"/>
<many-to-one name="dept" column="dept_id"></many-to-one>
</class>
</hibernate-mapping>
更改hibernate配置文件
hibernate.cfg.xml
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<!-- Generated by MyEclipse Hibernate Tools. -->
<hibernate-configuration>
<session-factory>
<property name="dialect">
org.hibernate.dialect.MySQLDialect
</property>
<property name="connection.url">
jdbc:mysql://localhost:3306/hibernate
</property>
<property name="connection.username">root</property>
<property name="connection.password">123456</property>
<property name="connection.driver_class">
com.mysql.jdbc.Driver
</property>
<property name="show_sql">true</property>
<property name="hbm2ddl.auto">update</property>
<mapping resource="www/hbsi/net/one2manyboth/Department.hbm.xml" />
<mapping resource="www/hbsi/net/one2manyboth/Employee.hbm.xml" />
</session-factory>
</hibernate-configuration>
增加
One2manyboth.java(1)一方放弃维护
invarse属性的意思是:你要放弃维护吗,设置成true,默认为false
Department.hbm.xml
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<!-- Generated by MyEclipse Hibernate Tools. -->
<hibernate-configuration>
<session-factory>
<property name="dialect">
org.hibernate.dialect.MySQLDialect
</property>
<property name="connection.url">
jdbc:mysql://localhost:3306/hibernate
</property>
<property name="connection.username">root</property>
<property name="connection.password">123456</property>
<property name="connection.driver_class">
com.mysql.jdbc.Driver
</property>
<property name="show_sql">true</property>
<property name="hbm2ddl.auto">update</property>
<mapping resource="www/hbsi/net/one2manyboth/Department.hbm.xml" />
<mapping resource="www/hbsi/net/one2manyboth/Employee.hbm.xml" />
</session-factory>
</hibernate-configuration>
One2manyboth.java
package www.hbsi.net.one2manyboth;
import org.hibernate.Session;
import org.junit.Test;
import www.hbsi.net.util.HibernateSessionFactory;
public class One2manyboth {
@Test
public void add() {
Session session = HibernateSessionFactory.getSession();
session.beginTransaction();
//创建部门
Department dept = new Department();
dept.setName("部门1");
//创建雇员1
Employee emp1 = new Employee();
emp1.setUsername("雇员1");
//告诉hibernate部员是哪个部门
emp1.setDept(dept);
//创建雇员2
Employee emp2 = new Employee();
emp2.setUsername("雇员2");
emp2.setDept(dept);
session.save(emp1);
session.save(emp2);
session.save(dept);
session.getTransaction().commit();
HibernateSessionFactory.closeSession();
}
}
![](https://img-my.csdn.net/uploads/201301/05/1357362952_8976.jpg)
![](https://img-my.csdn.net/uploads/201301/05/1357362274_5061.jpg)
![](https://img-my.csdn.net/uploads/201301/05/1357362283_3626.jpg)
One2manyboth.java(2)级联增加
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="www.hbsi.net.one2manyboth">
<class name="Department" table="department">
<id name="id" column="id">
<generator class="native"/>
</id>
<property name="name" column="name" type="string"/>
<set name="emps" table="employee" inverse="true">
<key column="dept_id"/>
<one-to-many class="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="www.hbsi.net.one2manyboth">
<class name="Employee" table="employee">
<id name="id" column="id">
<generator class="native"/>
</id>
<property name="username" column="username" type="string"/>
<many-to-one name="dept" column="dept_id" cascade="all"></many-to-one>
</class>
</hibernate-mapping>
One2manyboth.java
package www.hbsi.net.one2manyboth;
import org.hibernate.Session;
import org.junit.Test;
import www.hbsi.net.util.HibernateSessionFactory;
public class One2manyboth {
@Test
public void add() {
Session session = HibernateSessionFactory.getSession();
session.beginTransaction();
// 创建部门
Department dept = new Department();
dept.setName("部门1");
// 创建雇员1
Employee emp1 = new Employee();
emp1.setUsername("雇员1");
// 告诉hibernate部员是哪个部门
emp1.setDept(dept);
// 创建雇员2
Employee emp2 = new Employee();
emp2.setUsername("雇员2");
emp2.setDept(dept);
session.save(emp1);
session.save(emp2);
session.getTransaction().commit();
HibernateSessionFactory.closeSession();
}
}
![](https://img-my.csdn.net/uploads/201301/05/1357366383_5687.jpg)
![](https://img-my.csdn.net/uploads/201301/05/1357365186_2357.jpg)
![](https://img-my.csdn.net/uploads/201301/05/1357365207_1575.jpg)
One2manyboth.java(3)级联删除_删除雇员时删除部门
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="www.hbsi.net.one2manyboth">
<class name="Department" table="department">
<id name="id" column="id">
<generator class="native"/>
</id>
<property name="name" column="name" type="string"/>
<set name="emps" table="employee" cascade="save-update">
<key column="dept_id"/>
<one-to-many class="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="www.hbsi.net.one2manyboth">
<class name="Employee" table="employee">
<id name="id" column="id">
<generator class="native"/>
</id>
<property name="username" column="username" type="string"/>
<many-to-one name="dept" column="dept_id" cascade="delete"></many-to-one>
</class>
</hibernate-mapping>
One2manyboth.java
package www.hbsi.net.one2manyboth;
import org.hibernate.Session;
import org.junit.Test;
import www.hbsi.net.util.HibernateSessionFactory;
public class One2manyboth {
@Test
public void add() {
Session session = HibernateSessionFactory.getSession();
session.beginTransaction();
// 创建部门
Department dept = new Department();
dept.setName("部门1");
// 创建雇员1
Employee emp1 = new Employee();
emp1.setUsername("雇员1");
// 告诉hibernate部员是哪个部门
emp1.setDept(dept);
// 创建雇员2
Employee emp2 = new Employee();
emp2.setUsername("雇员2");
emp2.setDept(dept);
session.save(emp1);
session.save(emp2);
session.getTransaction().commit();
HibernateSessionFactory.closeSession();
}
//级联删除
@Test
public void delete(){
Session session = HibernateSessionFactory.getSession();
session.beginTransaction();
Employee emp = (Employee) session.get(Employee.class,45);
session.delete(emp);
session.getTransaction().commit();
HibernateSessionFactory.closeSession();
}
}
![](https://img-my.csdn.net/uploads/201301/05/1357368102_4596.jpg)
删除前:
![](https://img-my.csdn.net/uploads/201301/05/1357368300_5496.jpg)
![](https://img-my.csdn.net/uploads/201301/05/1357368329_7117.jpg)
删除后:
![](https://img-my.csdn.net/uploads/201301/05/1357368346_8876.jpg)
![](https://img-my.csdn.net/uploads/201301/05/1357368355_7950.jpg)
One2manyboth.java(4)级联删除_删除部门时删除雇员
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="www.hbsi.net.one2manyboth">
<class name="Department" table="department">
<id name="id" column="id">
<generator class="native"/>
</id>
<property name="name" column="name" type="string"/>
<set name="emps" table="employee" cascade="save-update,delete">
<key column="dept_id"/>
<one-to-many class="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="www.hbsi.net.one2manyboth">
<class name="Employee" table="employee">
<id name="id" column="id">
<generator class="native"/>
</id>
<property name="username" column="username" type="string"/>
<many-to-one name="dept" column="dept_id"></many-to-one>
</class>
</hibernate-mapping>
One2manyboth.java
package www.hbsi.net.one2manyboth;
import org.hibernate.Session;
import org.junit.Test;
import www.hbsi.net.util.HibernateSessionFactory;
public class One2manyboth {
@Test
public void add() {
Session session = HibernateSessionFactory.getSession();
session.beginTransaction();
// 创建部门
Department dept = new Department();
dept.setName("部门1");
// 创建雇员1
Employee emp1 = new Employee();
emp1.setUsername("雇员1");
// 告诉hibernate部员是哪个部门
emp1.setDept(dept);
// 创建雇员2
Employee emp2 = new Employee();
emp2.setUsername("雇员2");
emp2.setDept(dept);
session.save(emp1);
session.save(emp2);
session.getTransaction().commit();
HibernateSessionFactory.closeSession();
}
//级联删除
@Test
public void delete(){
Session session = HibernateSessionFactory.getSession();
session.beginTransaction();
//Employee emp = (Employee) session.get(Employee.class,45);
//session.delete(emp);
Department dept = (Department) session.get(Department.class,26);
session.delete(dept);
session.getTransaction().commit();
HibernateSessionFactory.closeSession();
}
}
![](https://img-my.csdn.net/uploads/201301/05/1357368900_4609.jpg)
删除前:
删除后: