由于以下问题: Why not to use Spring's OpenEntityManagerInViewFilter
和
http://heapdump.wordpress.com/2010/04/04/should-i-use-open-session-in-view/
我想使用Springs OpenentityManagerInViewFilter的替代方案。这绝对是一个性能问题。如果我禁用OpenentityManagerInViewFilter,我偶尔会收到错误:
LazyInitializationException:42 - failed to lazily initialize a collection
答案 0 :(得分:2)
过滤器的一个替代方法是访问延迟加载的集合中的所有元素,然后通过请求将它们发送到视图。但是,此时您应该质疑是否需要急切地获取这些属性。
这是一些要演示的伪代码。
//Inside controller
Department dept = dao.findDepartment(id);
//This will load the entities, avoiding the exception.
for(Employee e: dept.getEmployees()){ //Assume employees are lazy loaded
e.getName();
}
request.setAttribute("department", dept); //In Spring this could be the model