休眠保存临时实例

时间:2018-10-15 14:29:58

标签: java hibernate spring-boot hibernate-entitymanager

所以我有两个具有双向@ManyToMany关系的实体-学生和课程。 我的学生班级是:

@Entity
@Table(name = "student")
public class Student {

@Id
@GeneratedValue
private int id;

@Column(name = "role", unique = true, nullable = false)
private String studentName;

@ManyToMany
@JoinTable(name = "student_course",
           joinColumns = {@JoinColumn(name = "student_id", 
referencedColumnName = "id")},
           inverseJoinColumns = {@JoinColumn(name = "course_id",
                                             referencedColumnName = 
"id")})
private Set<Course> course = new HashSet<>();

public String getStudentName() {
    return studentName;
}

public void setStudentName(final String studentName) {
    this.studentName = studentName;
}

public Set<Course> getCourses() {
    return courses;
}

public void setCourses(final Set<Course> courses) {
    this.courses = courses;
}

@Override
public int hashCode() {
    ...
}

@Override
public boolean equals(final Object obj) {
   ...
}

@Override
public String toString() {
    ...
}

我的课程类别为:

@Entity
@Table(name = "course")
public class Course {

@Id
@GeneratedValue
private int id;

@Column(name = "course", nullable = false, unique = true)
private String course;

@ManyToMany(fetch = FetchType.LAZY, mappedBy = "courses")
private Set<Student> students = new HashSet<>();

public String getCourse() {
    return course;
}

public void setCourse(final String course) {
    this.course = course;
}

public Set<Student> getStudents() {
    return students;
}

public void setStudents(final Set<Student> students) {
    this.students = students;
}

@Override
public int hashCode() {
    ...
}

@Override
public boolean equals(final Object obj) {
    ...
}

@Override
public String toString() {
    ...
}

使用休眠实体管理器,我想保留两个实体。因此,在我的DbInitializer()中,我有类似的内容可以填充课程实体的数据。

private Set<Course> initCourses() {
    final Set<Course> courses = 
    CourseProvider.getTestCourses();

    for (final Course course : courses) {
        entityManager.persist(course);
    }
    return courses;
}

这很好。但是,当我尝试为Student实体填充数据时,它要求我在刷新之前保存瞬态实例。

private List<Student> initStudents() {  
    final Set<Student> students = StudentProvider.getTestStudents();
    for (final Student student : students) {
        entityManager.persist(student);
    }
    return students;
}

StudentProvider类:

public class StudentProvider {

public static List<Student> getTestStudentsList() {

    final List<Student> students = 
    StudentFactory.createStudents();
    return students;
}

}

StudentFactory类:

public class StudentFactory {

public static final List<Student> createStudents() {

    final EnumSet<StudentEnum> students = 
    EnumSet.allOf(StudentEnum.class);
    final List<Student> studentList = new ArrayList<>();

    for (final StudentEnum s : students) {

        final Student student = new Student();
        student.setStudentName(s.toString());
        student.setCourses(s.transform());
        studentList.add(student);
    }

    return studentList;
}

StudentProvider调用为学生设置值的StudentFactory。 StudentEnum包含具有相应课程的学生列表。

我知道层叠= CascadeType.ALL可以解决此问题。我尝试使用它,但是它给我“课程实体的重复输入错误”。如果我不使用级联,它会要求我保存瞬态实例。

有人可以建议我不使用cascasde的方法吗??? 我了解到,在保留学生实体之前,必须保存课程实体。

我经历了几个Stackoverflow问题,但无法解决问题。需要帮助! 先感谢您! :)

0 个答案:

没有答案