Hibernate @OneToMany批注到底如何工作? [英] How exactly does the Hibernate @OneToMany annotation work?

查看:54
本文介绍了Hibernate @OneToMany批注到底如何工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对Hibernate来说还很陌生,我正在教程中对其进行研究.我在理解 OneToMany 注释的工作原理时遇到一些问题.

I am pretty new to Hibernate and I am studying it on a tutorial. I have some problems understanding how exactly the OneToMany annotation works.

因此,我有以下两个实体类:代表学生的学生和代表指导学生的人的指南.因此,每个学生都与一个指南相关联,但是一个指南可以跟随一个以上的学生.我想要一个向导来了解与他相关的学生.

So I have these 2 entity classes: Student that represents a student and Guide that represents a person that guides the student. So each student is associated with a single guide but a single guide can follow more that one student. I want a guide to know the students associated to him.

所以我有

学生:

@Entity
public class Student {

    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    private Long id;

    @Column(name="enrollment_id", nullable=false)
    private String enrollmentId;    

    private String name;

    @ManyToOne(cascade={CascadeType.PERSIST, CascadeType.REMOVE})
    @JoinColumn(name="guide_id")
    private Guide guide;

    public Student() {}
    public Student(String enrollmentId, String name, Guide guide) {
        this.enrollmentId = enrollmentId;
        this.name = name;
        this.guide = guide;
    }

    public Guide getGuide() {
        return guide;
    }
    public void setGuide(Guide guide) {
        this.guide = guide;
    }

}

因此,指南字段上的 @ManyToOne 注释:

So the @ManyToOne annotation on the guide field:

@ManyToOne(cascade={CascadeType.PERSIST, CascadeType.REMOVE})
@JoinColumn(name="guide_id")
private Guide guide; 

是指单个指南与单个学生相关联,但是一个指南可以跟随许多学生.这样对吗?指定的级联设置到底能做什么?我认为这意味着,当我保留包含 Guide 对象作为字段的Student对象时,该 Guide 对象也会自动保留.当我删除 Student 对象时,也会发生同样的事情,相关的 Guide 记录也将被删除...但是我对此不太确定...

means that a single guide is associated a single student but a guide can follow many students. Is it right? What exactly does the specified cascade settings do? I think it means that when I persist a Student object that contains a Guide object as field also this Guide object is also automatically persisted. And the same thing happens when I remove a Student object, the related Guide record is deleted...but I am not absolutely sure about it...

好吧,这样做,我将在 Student 表中的记录与 Guide 中的记录之间具有单向关系.表,因为在学生表中,我将有一个外键加入 Guide 表,以便学生可以了解其指南,但是这样做,该指南无法知道跟在后面的学生...这并不聪明.

Ok, doing it this way I will have a mono directional relationship between a record in the Student table and a record in the Guide table because in the Student table I will have a foreign key to join the Guide table so the student can know its guide but, doing it this way, the guide can not know the followed student...and this is not smart.

为此,指南类是通过以下方式实现的:

To do it the Guide class is implemented in this way:

@Entity
public class Guide {

    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    private Long id;

    @Column(name="staff_id", nullable=false)
    private String staffId; 

    private String name;
    private Integer salary;

    @OneToMany(mappedBy="guide", cascade={CascadeType.PERSIST})
    private Set<Student> students = new HashSet<Student>(); 

    public Guide() {}
    public Guide(String staffId, String name, Integer salary) {
        this.staffId = staffId;
        this.name = name;
        this.salary = salary;
    }

    public Set<Student> getStudents() {
        return students;
    }   
    public void setSalary(Integer salary) {
        this.salary = salary;
    }

    public void addStudent(Student student) {
        students.add(student);
        student.setGuide(this);
    }

}   

因此,如您所见,该类包含:

So, as you can see, this class contains:

@OneToMany(mappedBy="guide", cascade={CascadeType.PERSIST})
private Set<Student> students = new HashSet<Student>(); 

用于声明双向关系.

所以在我看来,此批注会自动在 Student 表中创建一个 guide_id 字段,该字段表示实现双向的外键关系.

So it seems to me that this annotation automatically create a guide_id field into the Student table that represent the foreign key that implement the bidirectional relation.

实际上,使用这种映射, Student 表是通过这种方式在我的数据库中自动创建的:

In fact using this mapping the Student table is automatically created in this way in my database:

'id', 'bigint(20)', 'NO', 'PRI', NULL, 'auto_increment'
'enrollment_id', 'varchar(255)', 'NO', '', NULL, ''
'name', 'varchar(255)', 'YES', '', NULL, ''
'guide_id', 'bigint(20)', 'YES', 'MUL', NULL, ''

因此,在 Student 实体类中,我没有定义 guide_id 字段,但在数据库的 Student 表中有此字段.因此,我认为表中此字段的创建取决于在 Guide 实体类中定义的先前的 @OneToMany 批注.那是正确的还是我错过了什么?

So in the Student entity class I have not defined the guide_id field but I have it in the Student table on the database. So I think that the creation of this field in the table depends on the previous @OneToMany annotation defined in the Guide entity class. Is that correct or am I missing something?

推荐答案

是的,您可以定义一个没有双向关联的 @OneToMany 实体,并且添加的列位于 Many 数据库中的实体端(即使该实体不知道它已链接到 One 端实体).

Yes, you can define a @OneToMany entity without a bidirectional association, and the added column is on the Many entity side in the database (even though the entity doesn't know it is linked to the One-side entity).

您也可以为此使用联接表,但这不是必需的.

You can also use a join table for this, but it's not necessary.

这篇关于Hibernate @OneToMany批注到底如何工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆