双向关联:
员工知道所属部门,部门知道包括哪些员工
1. package oneTomany;
3. import java.util.HashSet;
4. import java.util.Set;
6. public class Department {
7. private int id;
8. private String name;
9. private Set<Employee> employees=new HashSet<Employee>();
10. public int getId() {
11. return id;
12. }
13. public void setId(int id) {
14. this.id = id;
15. }
16. public String getName() {
17. return name;
18. }
19. public void setName(String name) {
20. this.name = name;
21. }
22. public Set<Employee> getEmployees() {
23. return employees;
24. }
25. public void setEmployees(Set<Employee> employees) {
26. this.employees = employees;
27. }
28. @Override
29. public String toString() {
30. // TODO Auto-generated method stub
31. return "[Department:id="+id+",]";
32. }
34. }
1. package oneTomany;
3. public class Employee {
4. private int id;
5. private String name;
6. private Department department; // 关联的对象
7. public int getId() {
8. return id;
9. }
10. public void setId(int id) {
11. this.id = id;
12. }
13. public String getName() {
14. return name;
15. }
16. public void setName(String name) {
17. this.name = name;
18. }
19. public Department getDepartment() {
20. return department;
21. }
22. public void setDepartment(Department department) {
23. this.department = department;
24. }
25. @Override
26. public String toString() {
27. // TODO Auto-generated method stub
28. return "[Employee:id="+id+",]";
29. }
30. }
Department.hbm.xml
<?xml version="1.0" encoding="gbk"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<!--
This mapping demonstrates
(1) a table-per-subclass mapping strategy
(2) a simple component mapping
(3) recursive associations withing an inheritance tree
-->
<hibernate-mapping package="oneTomany">
<!--
name:类名
table:表名
-->
<class name="Department" table="t_department">
<id name="id" type="int" column="id" >
<generator class="native"/> <!--表示自动增长-->
</id>
<property name="name" type="string" column="name" />
<!--employees属性 ,表达的是本类与Employee类的一对多的关系-->
<set name="employees" >
<key column="departmentId"></key>
<one-to-many class="Employee"></one-to-many>
</set>
</class>
</hibernate-mapping>
Employee.hbm.xml
<?xml version="1.0" encoding="gbk"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<!--
This mapping demonstrates
(1) a table-per-subclass mapping strategy
(2) a simple component mapping
(3) recursive associations withing an inheritance tree
-->
<hibernate-mapping package="oneTomany">
<!--
name:类名
table:表名
-->
<class name="Employee" table="t_employee">
<id name="id" type="int" column="id" >
<generator class="native"/> <!--表示自动增长-->
</id>
<property name="name" type="string" column="name" />
<!--Department属性,表达的是本类与Department的多对一的关系-->
<many-to-one name="department" class="Department" column="departmentId"></many-to-one>
</class>
</hibernate-mapping>
也可以单向关联,可以让一对多的一方放弃维护关联关系,在 set 标签中添加属性 inverse=”true”,inverse 默认为 false,为 true 时表示由对方维护关联关系,本方不维护。可以减少 sql 语句。
在保存的时候,被依赖的在前边保存,不被依赖的在后边保存
1. // 保存
2. session.save(department);
3. session.save(employee1);
4. session.save(employee2);
department 保存后,保存 employee 时就直接保存了外键的值,当先保存 employee 是,不知道外键的值,多以保存完 depatment 后,再保存外键的值。