1:配置式(xml)关联关系
我们用Dept(部门),Emp(员工)两张表来做下面的例子。
字段
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
public class Dept { private Integer deptno; private String deptname; //员工实体 一对多 private Set<Emp>emps=new HashSet<Emp>(); public Set<Emp> getEmps() { return emps; } public void setEmps(Set<Emp> emps) { this.emps = emps; } public Integer getDeptno() { return deptno; } public void setDeptno(Integer deptno) { this.deptno = deptno; } public String getDeptname() { return deptname; } public void setDeptname(String deptname) { this.deptname = deptname; } }
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
public class Emp { private Integer empno; private String empname; private Integer deptno; //植入部门实体 private Dept dept=new Dept(); public Integer getDeptno() { return deptno; } public void setDeptno(Integer deptno) { this.deptno = deptno; } public Dept getDept() { return dept; } public void setDept(Dept dept) { this.dept = dept; } public Integer getEmpno() { return empno; } public void setEmpno(Integer empno) { this.empno = empno; } public String getEmpname() { return empname; } public void setEmpname(String empname) { this.empname = empname; } }
1, 一对多 one-to-many
重点1 :在少的一方(Dept)植入多的一方的 Set 集合
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="cn.happy.de">
<!--dynamic-update="true" 动态更新先检索再修改-->
<class name="cn.happy.de.Dept" table="Dept" dynamic-update="true" >
<id name="deptno" column="deptno">
<generator class="native"></generator>
</id>
<property name="deptname" column="deptname"></property>
<!--一对多双向关联 //员工实体 一对多private Set<Emp>emps;-->
<!-- cascade="save-update"级联 -->
<!-- inverse="true"放弃维护关系:不干预顶层集合数据 -->
<!-- order-by="empno desc"排序 -->
<set name="emps" cascade="save-update,delete" inverse="true" order-by="empno desc">
<key column="deptno"></key>
<one-to-many class="Emp"></one-to-many>
</set>
</class>
</hibernate-mapping>
重点2 :在多的一方(Emp)植入少的一方的对象
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="cn.happy.de">
<!--dynamic-update="true" 动态更新先检索再修改-->
<class name="cn.happy.de.Emp" table="Emp" dynamic-update="true" >
<id name="empno" column="empno">
<generator class="native"></generator>
</id>
<property name="empname" column="empname"></property>
<!--
name:在多的一方植入对方的对象名
class:再多的一方植入对方的类型名
column:在当前分析的对象对应底层表的外列的值
-->
<many-to-one name="Dept" class="Dept" column="deptno" cascade="save-update"></many-to-one>
</class>
</hibernate-mapping>
测试:修改
@Test
public void s2(){
Dept dept=new Dept();
dept.setDeptno(1);
Emp emp=HQLutil.getSession().load(Emp.class,2);
emp.setDept(dept);
HQLutil.getSession().saveOrUpdate(emp);
Transaction transaction = HQLutil.getSession().beginTransaction();
transaction.commit();
}
2, 多对多 many-to-many
Employee(员工), Project(项目)表
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
public class Employee { private Integer empid; private String empname; private Set<Project>projects=new HashSet<Project>(); public Integer getEmpid() { return empid; } public void setEmpid(Integer empid) { this.empid = empid; } public String getEmpname() { return empname; } public void setEmpname(String empname) { this.empname = empname; } public Set<Project> getProjects() { return projects; } public void setProjects(Set<Project> projects) { this.projects = projects; } }
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
public class Project { private Integer proid; private String proname; public Set<Employee> getEmployees() { return employees; } public void setEmployees(Set<Employee> employees) { this.employees = employees; } private Set<Employee>employees=new HashSet<Employee>(); public Integer getProid() { return proid; } public void setProid(Integer proid) { this.proid = proid; } public String getProname() { return proname; } public void setProname(String proname) { this.proname = proname; } }
重点1 :Employee每方都植入 Set 集合
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="cn.happy.er">
<!--dynamic-update="true" 动态更新先检索再修改-->
<class name="cn.happy.er.Employee" table="Employee" dynamic-update="true">
<id name="empid" column="empid">
<generator class="native"></generator>
</id>
<property name="empname" column="empname"></property>
<!--table:中间表-->
<set name="projects" table="PROEMP" cascade="save-update" lazy="extra">
<!-- column:植入当前我们依赖的id列 -->
<key column="REMPID"></key>
<!-- column:植入对方我们依赖的id列 -->
<many-to-many class="Project" column="RPROID"></many-to-many>
</set>
</class>
</hibernate-mapping>
重点2 :Project每方都植入 Set 集合
<?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="cn.happy.er"> <!--dynamic-update="true" 动态更新先检索再修改--> <class name="cn.happy.er.Project" table="Project" dynamic-update="true" > <id name="proid" column="proid"> <generator class="native"></generator> </id> <property name="proname" column="proname"></property> <set name="employees" table="PROEMP" cascade="save-update" > <key column="RPROID"></key> <many-to-many class="Employee" column="REMPID"></many-to-many> </set> </class> </hibernate-mapping>
2:注解式关联关系
注解:
@Entity :持久化类
@Table :数据库表名
@Id :主键
@GeneratedValue :主键生成策略
@JoinColumn(name = "dids") :主表在字表中的外键
@OneToOne(mappedBy = "dept",cascade = CascadeType.ALL) mapperBy哪个类拥有这个属性就维护表关系
@ManyToMany(cascade = CascadeType.ALL)
@JoinTable(name = "PROEMP",joinColumns = {@JoinColumn(name = "REMPID")} :生成中间表
,inverseJoinColumns = {@JoinColumn(name = "RPROID")}
)