![](https://csdnimg.cn/release/download_crawler_static/86358289/bg1.jpg)
以员工与部门为例
1. 创建两个类
2. Employee.Java
3. import java.io.Serializable;
4.
5. public class Employee implements Serializable { //一定要实现Serializable接
口,这是属于多的一方
6.
7. private int empId;
8. private String empName;
9. private double salary;
10. private Dept dept;
11. //这里自己添加get和set方法
12. //也可以添加toString方法
13. }
Dept.java 类
import java.util.HashSet;
import java.util.Set;
public class Dept {
private int deptId;
private String deptName;
private Set<Employee> emps=new HashSet<Employee>();
14. //这里自己添加get和set方法
15. //也可以添加toString方法
}
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory >
<!-- 数据库配置 -->
<property
name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property
name="hibernate.connection.url">jdbc:mysql://localhost:3306/empresa?serverT
imezone=UTC</property>