我有一個通用條件查詢,它會返回相同的記錄。我認爲我的學生保存方法有問題。這是我的保存方法;休眠條件示例查詢獲取多條記錄
Student student = new Student();
student.setId(Utility.generateUUID());
student.setClassroom(selectedClassroom);
student.setUrl(urlAddress);
genericService.save(student);
當我試圖從數據表,它返回3教室對象,該對象是相同的,但只有一個在課堂表記錄所有教室。問題是有3個學生記錄哪個教室引用了這個課堂記錄。
我的標準查詢;
@Transactional(readOnly = true)
public <T> List<T> getByTemplate(T templateEntity) {
Criteria criteria = getCurrentSession().createCriteria(templateEntity.getClass());
criteria.add(Example.create(templateEntity));
return criteria.list();
}
實體;
public class Classroom{
....
@OneToMany(mappedBy = "classroom", fetch = FetchType.EAGER)
private List<Student> studentList;
}
public class Student{
@JoinColumn(name = "classroom", referencedColumnName = "id")
@ManyToOne(fetch = FetchType.LAZY)
private Classroom classroom;
}
你爲什麼要手動設置'student'' id,你不讓'Hibernate'爲你管理它? – Atropo