gpt4 book ai didi

Java - Hibernate @ManyToMany 映射仅在一个方向上添加数据库中的记录

转载 作者:搜寻专家 更新时间:2023-10-30 20:41:13 24 4
gpt4 key购买 nike

我是 Hibernate 的新手,我遇到了一个问题。我有两个实体(学生和类(class))。学生可以有很多类(class),类(class)可以有很多学生。当我制作一门新类(class)并将其添加到数据库中并在同一 session 中我将这门类(class)添加到学生的类(class)中时(student.getCourses()。添加(类(class)))一切正常,并且在类(class)中都添加了记录表和 Students_Courses 表。当我在另一个方向上做同样的事情时,它无法正常工作。首先,我在数据库中添加了一个新学生。我参加现有类(class)并执行 (course.getStudents().add(student)) 但没有记录保存在 Students_Courses 表中。多对多映射都是 PERSIST 级联,因此它必须工作。

你知道问题可能是什么吗?我正在使用 MSSQL 数据库。

第一个方向(不工作——只有新学生被保存在数据库中)

Session session = HibernateUtils.getSessionFactory().openSession();
session.beginTransaction();
{
Faculty faculty = (Faculty) session.get(Faculty.class, 1);

Student newStudent = new Student();
newStudent.setFaculty(faculty);
newStudent.setGrade(3.0);
newStudent.setName("Some student name");

session.save(newStudent);

Course course = (Course) session.get(Course.class, 1);
course.getStudents().add(newStudent);
// session.update(course); This does not have any effect because the course is persistent anyway

session.getTransaction().commit();
session.close();
}

第二个方向(工作正常 - 新类(class)和中间表中的记录都保存在数据库中)

Session session = HibernateUtils.getSessionFactory().openSession();
session.beginTransaction();
{
Course newCourse = new Course();
newCourse.setName("Some course name");

session.save(newCourse);

Student student = (Student) session.get(Student.class, 1);
student.getCourses().add(newCourse);

session.getTransaction().commit();
session.close();
}

这是学生实体

@Entity(name = "Students")
public class Student {
@Id
@GeneratedValue
@Column(name = "StudentId")
private int id;

@Column(name = "Name", nullable = false, length = 50)
private String name;

@Column(name = "Grade")
private Double grade = null;

@ManyToOne(cascade = CascadeType.PERSIST)
@JoinColumn(name = "FacultyId", nullable = false)
private Faculty faculty;

@ManyToMany(cascade = CascadeType.PERSIST)
@JoinTable(
joinColumns = @JoinColumn(name = "StudentId"),
inverseJoinColumns = @JoinColumn(name = "CourseId"))
private Collection<Course> courses;

public Student() {
this.courses = new HashSet<Course>();
}

// Setters and Getters for all fields
}

和类(class)实体

@Entity(name = "Courses")
public class Course {
@Id
@GeneratedValue
@Column(name = "CourseId")
private int id;

@Column(name = "Name", nullable = false, length = 100)
private String name;

@ManyToMany(mappedBy = "courses", cascade = CascadeType.PERSIST)
private Collection<Student> students;

public Course() {
this.students = new HashSet<Student>();
}

// Setters and Getters for all fields
}

最佳答案

请参阅以下内容:

Cascade.ALL from non-owning entity side

我还建议封装添加/删除操作。这些集合的 setter/getter 是否返回不可修改的集合并强制客户使用以下内容以便正确设置关系:

public class Course{
public void addStudent(Student student){
this.students.add(student);
student.getCourses().add(this);
}
}

public class Student{
public void addCourse(Course course){
this.courses.add(course);
course.students.add(this);
}
}

关于Java - Hibernate @ManyToMany 映射仅在一个方向上添加数据库中的记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19644045/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com