【发布时间】:2014-12-10 03:34:08
【问题描述】:
我需要帮助以找到解决此错误的方法:
org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: org.example.myproject.domains.Person.tasks, could not initialize proxy - no Session
at org.hibernate.collection.internal.AbstractPersistentCollection.throwLazyInitializationException(AbstractPersistentCollection.java:575)
at org.hibernate.collection.internal.AbstractPersistentCollection.withTemporarySessionIfNeeded(AbstractPersistentCollection.java:214)
at org.hibernate.collection.internal.AbstractPersistentCollection.readSize(AbstractPersistentCollection.java:155)
at org.hibernate.collection.internal.PersistentSet.isEmpty(PersistentSet.java:166)
at com.lyncode.jtwig.content.model.compilable.For$Compiled.handleCollection(For.java:132)
at com.lyncode.jtwig.content.model.compilable.For$Compiled.render(For.java:98)
at com.lyncode.jtwig.content.model.compilable.Sequence$Compiled.render(Sequence.java:80)
at com.lyncode.jtwig.content.model.renderable.Replacement.render(Replacement.java:32)
at com.lyncode.jtwig.content.model.compilable.Sequence$Compiled.render(Sequence.java:80)
at com.lyncode.jtwig.parser.JtwigParser$CompiledDocument.render(JtwigParser.java:67)
at com.lyncode.jtwig.parser.JtwigParser$CompiledDocument.render(JtwigParser.java:67)
at com.lyncode.jtwig.mvc.JtwigView.renderMergedTemplateModel(JtwigView.java:102)
at org.springframework.web.servlet.view.AbstractTemplateView.renderMergedOutputModel(AbstractTemplateView.java:167)
at org.springframework.web.servlet.view.AbstractView.render(AbstractView.java:303)
...
当我尝试输入时服务器抛出此异常:http://localhost:8080/myproject/persons/1
控制器是:
@RequestMapping("/persons/{id}")
public ModelAndView updateMemeber(@PathVariable Integer id) {
ModelAndView mav = new ModelAndView("user/show");
mav.addObject("title", "Show User");
mav.addObject("person", personService.get(id));
return mav;
}
模型的定义如下:
人物模型:
@Entity
@Table(name="persons")
public class Person {
/**
* The rest of attributes.
*
*/
@OneToMany(fetch = FetchType.LAZY, mappedBy="person")
public Set<Task> tasks;
/**
* get a set of tasks
*/
public Set<Task> getTasks() {
return this.tasks;
}
}
任务模型:
@Entity
@Table(name="tasks")
public class Task {
/**
* get a set of tasks
*/
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name="person_id")
private Person person;
}
我阅读了很多关于这个问题的信息,但任何解决方案都对我有用;如果您需要更多信息,请告诉我。
【问题讨论】:
标签: java hibernate spring-mvc