这是我之前的问题的延续:Spring MVC with Hibernate Data Saving Error。
根据那里的答案解决了先前的错误。现在我想在表单中显示保存的值。
@RequestMapping("manage")
public String manage(@ModelAttribute Automotive automotive,
BindingResult result, ModelMap model) {
List<Company> companies = companyService.listAllCompanies();
model.addAttribute("companies", companies);
automotiveService.addAutomotive(automotive);
return "automotive/index";
}
我的观点是
<form:form action="/Automotive/manage" modelAttribute="automotive">
<form:hidden path="id" />
Name : <form:input path="name" />
Description : <form:input path="description" />
Type : <form:input path="type" />
Company : <form:select path="company" items="${companies}"
itemLabel="name" itemValue="id" title="Company"/>
<input type="submit" />
</form:form>
我能够正确保存数据,但返回时显示抛出错误:
org.apache.jasper.JasperException: org.hibernate.LazyInitializationException: could not initialize proxy - no Session
root cause: org.hibernate.LazyInitializationException: could not initialize proxy - no Session
org.hibernate.proxy.AbstractLazyInitializer.initialize(AbstractLazyInitializer.java:149)
org.hibernate.proxy.AbstractLazyInitializer.getImplementation(AbstractLazyInitializer.java:195)
org.hibernate.proxy.pojo.javassist.JavassistLazyInitializer.invoke(JavassistLazyInitializer.java:185)
biz.canisrigel.automirror.model.Company_$$_javassist_0.toString(Company_$$_javassist_0.java)
org.springframework.util.ObjectUtils.nullSafeToString(ObjectUtils.java:610)
更新
在使用 Kevin 对 OpenSessioninViewFilter 的建议时,由于我使用的是会话工厂接口,因此我得到了 No qualifying bean of type [javax.persistence.EntityManagerFactory] is defined 的错误。