[英]JPA: OneToMany relationship keeps the Collection empty
It seems I am having a difficult time understanding JPA and how the OneToMany relationship actually works. 似乎我在理解JPA以及OneToMany关系如何实际工作方面遇到了困难。
For example, imagine I have an object Class
例如,假设我有一个对象
Class
@Entity
public class Class {
@Id
private String className;
@OneToMany(cascade = Cascade.ALL, orphanRemoval = true)
private Set<Student> students;
// Constructors, Getters, Setter
}
I also have an object Student
where it holds Class. 我还有一个对象
Student
,该类位于Class中。
@Entity
public class Student {
@Id
private String studentName;
@ManyToOne
private Class class;
// Constructors, Getters, Setter
}
Obviously, a student can have multiple classes but forget about that. 显然,一个学生可以有多个班级,但不要管它了。
Why is that when I build a class and then build a couple students using that class, findAll()
on the ClassRepository
returns me an empty set of students. 为什么当我建立一个班级然后使用该班级建立几个学生时,
ClassRepository
上的findAll()
返回一个空的学生集。
Class class = new Class("CS", new HashSet<>());
classRepository.save(class); // repository has no special functions
Student student1 = new Student("1", class);
Student student2 = new Student("2", class);
studentRepository.save(student1);
studentRepository.save(student2);
classRepository.findAll() // Returns me a List<Class> with only one class object that has an empty set.
I was thinking the above code should automatically see that the two students are from that one class and so when I call buildingRepository.findAll()
, it will return a Class
object with the students set populated properly. 我以为上面的代码应该自动看到两个学生来自一个班级,因此当我调用
buildingRepository.findAll()
,它将返回一个Class
对象,并正确填充了学生集。
Is my understanding wrong then? 那我的理解错了吗? Or is my code wrong?
还是我的代码错了? And how can I change it up to fix it?
以及如何更改它以进行修复?
You can choose: 你可以选择:
1. Unidirectional @OneToMany
: 1.单向
@OneToMany
:
@Entity
public class Class {
@Id
private String className;
@OneToMany(cascade = Cascade.ALL, orphanRemoval = true)
private List<Student> students=new ArrayList<>();
// Constructors, Getters, Setter
}
@Entity
public class Student {
@Id
private String studentName;
// Constructors, Getters, Setter
}
Now, if we persist one Class
: 现在,如果我们坚持一个
Class
:
Class class1=new Class("name1");
class1.getStudents().add(new Student("student1Name"));
// then you can make a save of class1 in db
classRepository.save(class);
2. Unidirectional @OneToMany
with @JoinColumn
: 2.单向
@OneToMany
与@JoinColumn
:
To fix the aforementioned extra join table issue, we just need to add the @JoinColumn
in the mix: 要解决上述额外的
@JoinColumn
表问题,我们只需要在组合中添加@JoinColumn
:
@OneToMany(cascade = CascadeType.ALL, orphanRemoval = true)
@JoinColumn(name = "class_id")
private List<Student> students = new ArrayList<>();
3. Bidirectional @OneToMany
: 3.双向
@OneToMany
:
The best way to map a @OneToMany association is to rely on the @ManyToOne side to propagate all entity state changes: 映射@OneToMany关联关系的最佳方法是依靠@ManyToOne端传播所有实体状态更改:
@Entity
public class Class {
@Id
private String className;
@OneToMany(
mappedBy = "class",
cascade = CascadeType.ALL,
orphanRemoval = true
)
private List<Student> students=new ArrayList<>();
// Constructors, Getters, Setter
public void addStudent(Student student) {
students.add(student);
student.setClass(this);
}
public void removeStudent(Student student) {
students.remove(student);
student.setClass(null);
}
}
@Entity
public class Student {
@Id
private String studentName;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "class_id")
private Class class;
}
And to persist: 并坚持:
Class c1=new Class("className1");
c1.addStudent(new Student("StudentNAme1"));
c1.addStudent(new Student("StudentNAme2"));
c1.addStudent(new Student("StudentNAme3"));
classRepository.save(c1);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.