避免休眠懒惰初始化异常 [英] avoid hibernate lazy initialization exception
问题描述
我有两个课程叫做讲师和课程。我的讲师类有一个课程对象列表变量,如图所示
@OneToMany(mappedBy =lecturer)
@NotFound(action = NotFoundAction.IGNORE)
private List< Course> courseList = new ArrayList< Course>();
在我的课程中,我有一个Lecturer对象作为下面的变量。
@ManyToOne
@JoinTable(name =course_lecturer,joinColumns = @JoinColumn(name =course_id),inverseJoinColumns = @JoinColumn name =lecturer_id))
私人讲师讲师;
现在我有一个Spring Controller方法,返回Lecturer的json对象,如下所示。
@RequestMapping(value =/ getlecturerbyid,method = RequestMethod.POST)
public @ResponseBody
Object getLecturer @ModelAttribute(value =id)讲师讲师){
Map< String,Object> response = new HashMap< String,Object>();
response.put(message,succeess);
response.put(讲师,讲师);
返回响应;
}
问题是抛出一个懒惰的初始化异常。因此,我将Lecturer和Course class中的两个变量的获取类型设置为渴望。现在问题是它无限地尝试提取对象(如果我取消任何一个,它会引发同样的异常)。
另一方面,如果我删除变量的任何一个类,然后休眠不知道这是另一个关系。例如:如果我从Lecturer中删除课程列表并删除其中一个Lecturer对象,则hibernate不会删除与已删除的Lecturer对象相关的课程对象。所以我必须编写一个hibernate查询并在代码中手动删除它。
您能否就如何解决这个问题向我推荐?
这是因为循环引用(父 - >子 - >父)。我相信你有这个想法。
解决问题的最简单方法是迭代课程列表并将 Lecturer
设置为null,然后放入进入响应
来打破如下的循环关系:
Object getLecturer @ModelAttribute(value =id)讲师讲师){
Map< String,Object> response = new HashMap< String,Object>();
response.put(message,succeess);
//断开循环关系
if(lecturer.getCourses()!= null){
for(Course course:lecturer.getCourses()){
course.setLecturer(NULL);
}
}
response.put(讲师,讲师);
返回响应;
}
I have two classes called Lecturer and Course. My Lecturer class has a List of Course objects a variable as shown
@OneToMany(mappedBy = "lecturer")
@NotFound(action = NotFoundAction.IGNORE)
private List<Course> courseList = new ArrayList<Course>();
In my course class I have a Lecturer object as a variable as below.
@ManyToOne
@JoinTable(name = "course_lecturer", joinColumns = @JoinColumn(name = "course_id"), inverseJoinColumns = @JoinColumn(name = "lecturer_id"))
private Lecturer lecturer;
Now I have a spring Controller method that returns a json object of Lecturer as below.
@RequestMapping(value = "/getlecturerbyid", method = RequestMethod.POST)
public @ResponseBody
Object getLecturer(@ModelAttribute(value = "id") Lecturer lecturer) {
Map<String, Object> response = new HashMap<String, Object>();
response.put("message", "succeess");
response.put("lecturer", lecturer);
return response;
}
The problem is it throws a lazy initialization exception. Therefore I set fetch type in both variables in Lecturer and Course class to eager. Now the problem is it infinitely tries to fetch objects eagerly(if I unset fetch of either, it raises the same exception).
On the other hand if I remove the variable of either class, then the hibernate does not know that there is a relationship for another. eg: if I remove course list from Lecturer and delete one of the Lecturer objects then hibernate does not remove the course objects that are related with the deleted Lecturer object. so i have to write a hibernate query and remove it manually in the code.
Can you please suggest me on how to over come this issue?
This is happening because of circular reference(parent -> child ->parent). I am sure you have got the idea.
The simplest solution for your problem is to iterate the list of courses and set the Lecturer
as null before putting it into response
to break the circular relationship as below:
Object getLecturer(@ModelAttribute(value = "id") Lecturer lecturer) {
Map<String, Object> response = new HashMap<String, Object>();
response.put("message", "succeess");
//break the circular relationship
if(lecturer.getCourses() != null){
for(Course course: lecturer.getCourses()){
course.setLecturer(null);
}
}
response.put("lecturer", lecturer);
return response;
}
这篇关于避免休眠懒惰初始化异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!