欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Hibernate(7)关系映射和Cascade级联操作

程序员文章站 2022-05-02 08:20:04
...

1 关联关系映射

  • 多对一(Employee-Department)
  • 一对多(Department-Employee)
  • 一对一(Person-IdCard)
  • 多对多(teacher-student)
  • cascade(Employee-Department)

2 多对一(Employee-Department)

**domain文件:**Studcourse.java

public class Studcourse {
    private BigDecimal stucourseid;
    private Student student;
    private Course course;
    private BigDecimal grade;
}

**配置文件:**Studcourse.hbm.xml

<many-to-one name="student" class="com.test.domain.Student" fetch="select">
    <column name="SID" precision="22" scale="0" />
</many-to-one>

3 一对多(Department-Employee)

**domain文件:**Student.java

public class Student implements java.io.Serializable {
    private BigDecimal sid;
    private String sname;
    private Set studcourses = new HashSet(0);
}

**配置文件:**Student.hbm.xml

<set name="studcourses" inverse="true">
    <key>
        <column name="SID" precision="22" scale="0" />
    </key>
    <one-to-many class="com.test.domain.Studcourse" />
</set>

4 一对一(Person-IdCard)

有两种方式:基于主键的一对一;基于外键的一对一。

①基于主键的一对一

Hibernate(7)关系映射和Cascade级联操作
**domain文件:**Person.java和IdCard.java

class Person{
    private Integer id;
    private String name;
    private IdCard idCard;
}
class IdCard {
    private Integer id;
    private Date validateDte;
    private Person person;
}

配置文件:
Person.hbm.xml

<one-to-one name="idCard" class="com.test.domain.IdCard"></one-to-one>

IdCard.hbm.xml

<id name="id" type="java.lang.Integer">
    <generator class="foreign">
        <param name="property">person</param>
    </generator>
</id>
<one-to-one name="person" class="com.test.domain.Person" constrained="true"></one-to-one>

②基于外键的一对一

Hibernate(7)关系映射和Cascade级联操作
**domain文件:**Person.java和IdCard.java

class Person{
    private Integer id;
    private String name;
    private IdCard idCard;
}
class IdCard {
    private Integer id;
    private Date validateDte;
    private Person person;
}

配置文件:
Person.hbm.xml

<one-to-one name="idCard" class="com.test.domain.IdCard"></one-to-one>

IdCard.hbm.xml

<id name="id" type="java.lang.Integer">
    <generator class="assigned" />
</id>
// one-to-one实际上是many-to-one的一个特例
<many-to-one name="person" unique="true" />

5 多对多(teacher-student)

在操作和性能方面都不太理想,所以多对多的映射使用较少,实际使用中最好转换成一对多的对象模型;Hibernate会为我们创建中间关联表,转换成两个一对多。
domain文件:

public class Student implements java.io.Serializable {
    private BigDecimal sid;
    private String sname;
    private Set studcourses = new HashSet(0);
}
public class Course implements java.io.Serializable {
    private BigDecimal cid;
    private String cname;
    private Short ccredit;
    private Set studcourses = new HashSet(0);
}
public class Studcourse {
    private BigDecimal stucourseid;
    private Student student;
    private Course course;
    private BigDecimal grade;
}

配置文件:
Student.hbm.xml 和 Course.hbm.xml

<set name="studcourses" inverse="true">
    <key>
        <column name="S_ID" precision="22" scale="0" />
    </key>
    <one-to-many class="com.test.domain.Studcourse" />
</set>

Studcourse.hbm.xml

<many-to-one name="student" class="com.test.domain.Student" fetch="select">
    <column name="S_ID" precision="22" scale="0" />
</many-to-one>
<many-to-one name="course" class="com.test.domain.Course" fetch="select">
    <column name="C_ID" precision="22" scale="0" />
</many-to-one>

6 cascade(Employee-Department)

  • Cascade用来说明当对主对象进行某种操作(添加、修改、删除……)时(比如删除一个部门时,删除该部门所有员工),是否对其关联的从对象也作类似的操作。
  • 常用的cascade
    none, all, save-update, delete, lock, refresh, evict, replicate, persist, merge, delete-orphan(one-to-many)
  • 一般在many-to-one,many-to-many中不设置级联,在one-to-one, one-to-many中设置级联。
  • 在集合属性和普通属性中都能使用cascade
  • 一般讲,cascade配置在one-to-one(主对象一方), one-to-many(one的一方)
<set name="studcourses" inverse="true" cascade="all">
    <key>
        <column name="SID" precision="22" scale="0" />
    </key>
    <one-to-many class="com.test.domain.Studcourse" />
</set>