Error message: detached entity passed to persist: com. Ld. The entity. The Teacher, (seemingly object within the loop contains),
The code is as follows:
Entity class: the Teacher
@ the Entity
@ Data
Public class the Teacher {
@ Id
@ GeneratedValue (strategy=GenerationType. IDENTITY)
Long id;
String name;
The @onetomany (mappedBy="the teacher", cascade=CascadeType. ALL)
Private List
}
Entity classes: Student
@ the Entity
@ Data
Public class Student {
@ Id
@ GeneratedValue (strategy=GenerationType. IDENTITY)
private Long id;
private String name;
private Integer age;
@ Transient
Private Long teacherId;
@ ManyToOne (cascade=CascadeType. ALL the fetch=FetchType. LAZY)
@ JoinColumn (name="teacherId")
Private the Teacher the Teacher;
}
The test class:
@ Test
Void addStudent () {
Student Student=new Student ();
Student. SetAge (14);
Student. Elegantly-named setName (" small ");
The Teacher. The Teacher=teacherDao findById (1 l). The get ();
Student. SetTeacher (the teacher);
StudentDao. Save (student);
}