In Hibernate, you can specify a one-to-many or it's inverse many-to-one via @OneToMany
or @ManyToOne
annotations, respectively. But in the examples I see, every time you relate A to B, you need to also relate B to A. For instance, if Teacher
has a one-to-many relation with Course
(a teacher can teach many courses), do I need to:
teacher.getCourses().add(mathCourse);
teacher.getCourses().add(historyCourse);
as well as:
mathCourse.setTeacher(teacher);
historyCourse.setTeacher(teacher);
Or is it sufficient to just relate them one-way (and thus allow you to pick one of the above sets of relations)? In other words, could I just relate them via:
teacher.getCourses().add(mathCourse);
teacher.getCourses().add(historyCourse);
Why/why not? Thanks in advance!