Hibernate 一对一、一对多、多对多注解属性的总结

mappedBy:

所填内容必为本类在另一方的字段名。

表示:本类放弃控制关联关系,所有对关联关系的控制,如:建立、解除与另一方的关系,都由对方控制,本类不管。举个例子:

Teacher和Student之间是多对多关联关系,在Student端的teachers字段的@ManyToMany注解里面,配置属性mappedBy = "students"。表示:学生没有资格控制与老师的关联关系(如:建立关系、删除关系),只有老师才有资格控制关联关系。学生不能说:”我们两做朋友吧“。(即使说了,也无济于事,老师当学生没说。即Hibernate把学生说的当作耳边风,虽然Hibernate也不会抛出异常,程序照样能运行)。但是,老师可以说,“我们做朋友吧”。那么,学生只能无任何拒绝理由地接受。同样,如果学生说,”我们从此一刀两断吧“,那也是没用的。只有老师说:“滚,有多远,滚多远,为师今日将你逐出师门,再也不想见到你”,那么,这才能有效地解除关联关系(注意:这是一个对象(对应数据库里的一行记录)和一个对象之间的关系,而不是表和表之间的关联关系。一对师生关系的解除,不能影响整个社会的师生关系)。

测试代码:(这是完整类,后面只贴出test1的代码,其他部分不变)

public class MappedByTest {

    private static SessionFactory sessionFactory;

    private Session session;

    @BeforeClass
    public static void beforeTest(){
        sessionFactory = HibernateUtil.getSessionFactory();
    }
    @AfterClass
    public static void afterTest(){
        sessionFactory.close();
    }
    @Before
    public void beginTransaction(){
        session = sessionFactory.openSession();
        session.beginTransaction();
    }
    @After
    public void commitTransaction(){
        session.getTransaction().commit();
        session.close();
    }

    @Test
    public void execute(){
        test1();
    }

    public void test1(){
        Student student1 = new Student("学生张三",3);
        Student student2 = new Student("学生李四",4);
        Teacher teacher1 = new Teacher("老师王五",5);
        Teacher teacher2 = new Teacher("老师赵六",6);

        List<Student> students = new ArrayList<>();
        students.add(student1);
        students.add(student2);
        List<Teacher> teachers = new ArrayList<>();
        teachers.add(teacher1);
        teachers.add(teacher2);

        session.save(teacher1);
        session.save(teacher2);
        student1.setTeachers(teachers);
        session.save(student1);
    }
}

产生的结果:

----------------------------------------------------------------------------------------

然后,清空数据库,做第二次测试:

public void test1() {
        Student student1 = new Student("学生张三", 3);
        Student student2 = new Student("学生李四", 4);
        Teacher teacher1 = new Teacher("老师王五", 5);
        Teacher teacher2 = new Teacher("老师赵六", 6);

        List<Student> students = new ArrayList<>();
        students.add(student1);
        students.add(student2);
        List<Teacher> teachers = new ArrayList<>();
        teachers.add(teacher1);
        teachers.add(teacher2);

        session.save(student1);
        session.save(student2);
        teacher1.setStudents(students);
        session.save(teacher1);
    }

产生的结果:

可见:在Student类的teachers字段的@ManyToMany注解上配置属性mappedBy = "students",所有Student类产生的对象均不能建立和老师的关联关系,只有老师可以。

----------------------------------------------------------------------------------------

然后,清空数据库,做第三次测试:

public void test1(){
        Student student1 = new Student("学生张三",3);
        Student student2 = new Student("学生李四",4);
        Teacher teacher1 = new Teacher("老师王五",5);
        Teacher teacher2 = new Teacher("老师赵六",6);

        List<Student> students = new ArrayList<>();
        students.add(student1);
        students.add(student2);
        List<Teacher> teachers = new ArrayList<>();
        teachers.add(teacher1);
        teachers.add(teacher2);

        int stuId = 4;
        int teacherId = 6;
        Student persistentStudent = (Student)session.get(Student.class, stuId);
        Teacher persistentTeacher = (Teacher)session.get(Teacher.class, teacherId);
        persistentStudent.getTeachers().remove(persistentTeacher);
        session.update(persistentStudent);
    }

控制台打出的SQL语句:

Hibernate:
    select
        student0_.student_id as student_1_0_0_,
        student0_.age as age2_0_0_,
        student0_.name as name3_0_0_
    from
        student student0_
    where
        student0_.student_id=?
Hibernate:
    select
        teacher0_.teacher_id as teacher_1_1_0_,
        teacher0_.age as age2_1_0_,
        teacher0_.name as name3_1_0_
    from
        teacher teacher0_
    where
        teacher0_.teacher_id=?
Hibernate:
    select
        teachers0_.student_id as student_2_2_0_,
        teachers0_.teacher_id as teacher_1_2_0_,
        teacher1_.teacher_id as teacher_1_1_1_,
        teacher1_.age as age2_1_1_,
        teacher1_.name as name3_1_1_
    from
        ts_relation teachers0_
    inner join
        teacher teacher1_
            on teachers0_.teacher_id=teacher1_.teacher_id
    where
        teachers0_.student_id=?

产生的结果:

----------------------------------------------------------------------------------------

然后,清空数据库,做第四次测试:

public void test1(){
        Student student1 = new Student("学生张三",3);
        Student student2 = new Student("学生李四",4);
        Teacher teacher1 = new Teacher("老师王五",5);
        Teacher teacher2 = new Teacher("老师赵六",6);

        List<Student> students = new ArrayList<>();
        students.add(student1);
        students.add(student2);
        List<Teacher> teachers = new ArrayList<>();
        teachers.add(teacher1);
        teachers.add(teacher2);

        int stuId = 4;
        int teacherId = 6;
        Student persistentStudent = (Student)session.get(Student.class, stuId);
        Teacher persistentTeacher = (Teacher)session.get(Teacher.class, teacherId);
        persistentTeacher.getStudents().remove(persistentStudent);
        session.update(persistentTeacher);
    }

控制台打出的SQL语句:

Hibernate:
    select
        student0_.student_id as student_1_0_0_,
        student0_.age as age2_0_0_,
        student0_.name as name3_0_0_
    from
        student student0_
    where
        student0_.student_id=?
Hibernate:
    select
        teacher0_.teacher_id as teacher_1_1_0_,
        teacher0_.age as age2_1_0_,
        teacher0_.name as name3_1_0_
    from
        teacher teacher0_
    where
        teacher0_.teacher_id=?
Hibernate:
    select
        students0_.teacher_id as teacher_1_2_0_,
        students0_.student_id as student_2_2_0_,
        student1_.student_id as student_1_0_1_,
        student1_.age as age2_0_1_,
        student1_.name as name3_0_1_
    from
        ts_relation students0_
    inner join
        student student1_
            on students0_.student_id=student1_.student_id
    where
        students0_.teacher_id=?
Hibernate: --------------------------注意这条
    delete
    from
        ts_relation
    where
        teacher_id=?
Hibernate:
    insert
    into
        ts_relation
        (teacher_id, student_id)
    values
        (?, ?)

产生的结果:

可见:在Student类的teachers字段的@ManyToMany注解上配置属性mappedBy = "students",所有Student类产生的对象均不能删除和老师的关联关系,只有老师可以。

----------------------------------------------------------------------------------------

由此证明了之前所说的,mappedBy表示,本类防止控制和另一方的关联关系,所填内容必为本类在另一方的字段名。

时间: 2025-01-05 18:58:16

Hibernate 一对一、一对多、多对多注解属性的总结的相关文章

mybatis 一对一 一对多 多对多

一对一 一对多 多对多 原文地址:https://www.cnblogs.com/cwone/p/11909271.html

Hibernate 集合映射 一对多多对一 inverse属性 + cascade级联属性 多对多 一对一 关系映射

1 . 集合映射 需求:购物商城,用户有多个地址. // javabean设计 // javabean设计 public class User { private int userId; private String userName; // 一个用户,对应的多个地址 private Set<String> address; private List<String> addressList = new ArrayList<String>(); //private Str

django mysql 表的一对一 一对多 多对多

表结构的设计 一对一 多对一  将key写在多 多对多 外键: 描述  多对一的关系 写在多的一方 class Book(models.Model) title = models.CharField(max_length=32,unique=Ture) publisher = models.ForeignKey (to=Publisher,on_deleete=models.CASADE) publisher = models.ForeignKey(to='Publisher', on_dele

Hibernate 映射文件的配置 核心文件的配置 一对一 一对多 多对多 hibernate实现分页 Hibernate中session的关闭问题总结

以留言系统为实例 1 .配置映射文件:首先得引入约束dtd <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> 然后<hibernate-mapping></hibernate-mapping>映射标签 <

SQLAlchemy_定义(一对一/一对多/多对多)关系

目录 Basic Relationship Patterns One To Many One To One Many To Many Basic Relationship Patterns 基本关系模式 The imports used for each of the following sections is as follows: 下列的 import 语句,应用到接下来所有的代章节中: from sqlalchemy import Table, Column, Integer, Forei

表关系(一对一,一对多,多对多)

可以在数据库图表中的表之间创建关系,以显示一个表中的列与另一个表中的列是如何相链接的. 在一个关系型数据库中,利用关系可以避免多余的数据.例如,如果设计一个可以跟踪图书信息的数据库,您需要创建一个名为 titles 的表,它用来存储有关每本书的信息,例如书名.出版日期和出版社.您也可能保存有关出版社的信息,诸如出版社的电话.地址和邮政编码.如果您打算在 titles 表中保存所有这些信息,那么对于某出版社出版的每本书都会重复该出版社的电话号码. 更好的方法是将有关出版社的信息在单独的表,publ

hibernate 一对多|多对一

一对多:(一个客户对应多个联系人) 客户实体:Customer.java package cn.future.domain; import java.util.HashSet; import java.util.Set; public class Customer { /* * CREATE TABLE `cst_customer` ( `cust_id` BIGINT(32) NOT NULL AUTO_INCREMENT COMMENT '客户编号(主键)', `cust_name` VAR

hibernate.一对一关联

实体类关系 一对一 一对多 多对一 多对多 Hibernate提供了两种映射一对一关联关系的方式:按照外键映射和按照主键映射.下面以员工账号和员工档案表为例,介绍这两种映射方式,并使用这两种映射方式分别完成以下持久化操作: (1)保存员工档案的同时分配给员工一个账号. (2)加载员工档案的同时加载账号信息. 1.按照外键映射 步骤一:创建实体类Users1和Resume1 Users1创建如下: public class Users1 { private Integer userid; priv

关于hibernate一对一配置

实体类关系 一对一 一对多 多对一 多对多 Hibernate提供了两种映射一对一关联关系的方式:按照外键映射和按照主键映射.下面以员工账号和员工档案表为例,介绍这两种映射方式,并使用这两种映射方式分别完成以下持久化操作 Users1创建如下: public class Users1 { private Integer userid;  //用户编号 private String username;  //名称 private String userpass; //密码 private Resu