自动更新具有唯一列的多对多表
当我写这个问题时,我找到了解决方案,但无论如何我还是要问。也许有一种更简单的方法可以做到这一点。
我想从两个表中创建一个关联表。 我们称它们为学生和课程。两者都有一个 ID 和一个唯一的名称。
我想要的是第三个表(student_id 到 course_id),当我插入正在参加课程的学生时,它会自动更新。
这几乎是我发现的以下示例 此处:带有注释的 Hibernate 多对多,但我将 @Table 注释更改为@Table(name = "学生", uniqueConstraints = {@UniqueConstraint(columnNames = {"STUDENT_NAME" })})
课程也是如此。
我插入第一个学生
Set<Course> courses = new HashSet<Course>();
courses.add("Math); courses.add("Science");
Student alice = new Student("alice", courses);
这是另一个学生
Student bob= new Student("bob", courses);
它崩溃了,因为课程表中已经存在科学和数学课程,并且 Student_to_course 表不会更新。
我所知道的是在课程表中查询具有此名称的课程。如果有的话,我会将其添加到学生课程中,否则添加一个新课程。
List<Course> list = session.createCriteria(Course.class)
.add(Restrictions.eq("courseName", "Maths")).list();
if (list.size() > 0){
courses.add(list.get(0));
}
else {
courses.add(new Course("Maths"));
}
s.setCourses(courses);
所以这可行,但我认为必须有更好的方法来做到这一点。
如果表中已经存在同名课程,是否有办法告诉 hibernate 将课程从课程表添加到学生中?
while I wrote this question I figured out a solution but I'm going to ask anyway. Maybe there's an easier way to do it.
I want to make an association table from two tables.
Let's call them Student and Course. Both have an id and an unique name.
What I want is a third table (student_id to course_id) wich is automatically updated when i insert a student who's attending courses.
It is almost the following example i found here: Hibernate Many-to-Many with Annotations but i changed the @Table annotation to@Table(name = "STUDENT",
uniqueConstraints = {@UniqueConstraint(columnNames = {"STUDENT_NAME" })})
The same for Courses.
I insert a first student
Set<Course> courses = new HashSet<Course>();
courses.add("Math); courses.add("Science");
Student alice = new Student("alice", courses);
Here comes another Student
Student bob= new Student("bob", courses);
And it crashes becauses the courses science and math already exists in the table courses and the student_to_course table will not be updated.
What I'm doing know is to query for a course with this name in the course table. If there is one i add it to the students course otherwise add a new one.
List<Course> list = session.createCriteria(Course.class)
.add(Restrictions.eq("courseName", "Maths")).list();
if (list.size() > 0){
courses.add(list.get(0));
}
else {
courses.add(new Course("Maths"));
}
s.setCourses(courses);
So this works, but I think there must be a better way to do it.
Is there a way to tell hibernate to add the courses to the student from the courses table if there already is a course with the same name in the table?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你这样做是正确的,没有其他办法。
但这种情况不太现实。大多数时候,可用课程列表是预先知道的,您只能在创建学生时在可用课程中进行选择。 UI 通常会提出一个多选框,您必须在其中选择课程,并且您不会使用其名称而是使用其 ID(或直接使用课程实体)来引用所选课程。
因此,该方法将如下所示:
或像这样:
在第一种情况下,获取一组课程的最简单、最快的方法当然是循环遍历课程 ID 并调用
session.load(courseId)
获得课程的参考,甚至不需要访问数据库。You're doing it correctly, and there's no other way.
Except this scenario is not very realistic. Most of the time, the list of available courses is known in advance, and you may only pick among the available courses when you create a student. The UI will typically propose a multi-select box where you'll have to select courses, and you won't refer to the chosen courses using their name, but using their ID (or using the Course entity directly).
The method will thus look like this:
or like this:
In the first case, the easiest and fastest way to get a set of course is to loop over the course IDs and call
session.load(courseId)
to get a reference to the course, without even needing to hit the database.