Why am I getting a Hibernate LazyInitializationException in this Spring MVC web application when the data displays correctly?

I am attempting to create a web application using Spring MVC, with Hibernate as its ORM layer. However, due to my inexperience with both frameworks I'm struggling. The following code will properly display all the records I am looking for but still throw a stack trace into my logs. I'm having trouble finding thorough documentation concerning integrating Hibernate and SpringMVC (I've looked on springsource.org and read various articles on the interweb). Could anyone point out what I could be doing wrong here? Please note that I have spent a few trying to track down answers on the internet for this, including looking at [this][1] SO question. Which was unfortunately no help. I should also note that the ORM part of this application has been used and tested in a stand alone Java application without problems. So I believe the integration of Spring MVC and Hibernate is causing the issue. Here is the stack trace (truncated) with the famous lazy initialization issue; 2009-03-10 12:14:50,353 [http-8084-6] ERROR org.hibernate.LazyInitializationException.(LazyInitializationException.java:19) - could not initialize proxy - no Session org.hibernate.LazyInitializationException: could not initialize proxy - no Session at org.hibernate.proxy.AbstractLazyInitializer.initialize(AbstractLazyInitializer.java:57) at org.hibernate.proxy.AbstractLazyInitializer.getImplementation(AbstractLazyInitializer.java:111) at org.hibernate.proxy.pojo.cglib.CGLIBLazyInitializer.invoke(CGLIBLazyInitializer.java:150) at com.generic.orm.generated.SearchRule$$EnhancerByCGLIB$$92abaed6.toString() at java.lang.String.valueOf(String.java:2827) at java.lang.StringBuffer.append(StringBuffer.java:219) at org.apache.commons.lang.builder.ToStringStyle.appendDetail(ToStringStyle.java:578) at org.apache.commons.lang.builder.ToStringStyle.appendInternal(ToStringStyle.java:542) at org.apache.commons.lang.builder.ToStringStyle.append(ToStringStyle.java:428) at org.apache.commons.lang.builder.ToStringBuilder.append(ToStringBuilder.java:840) at org.apache.commons.lang.builder.ReflectionToStringBuilder.appendFieldsIn(ReflectionToStringBuilder.java:606) ..... Here is a code from my web page controller; private List getReports() { Session session = HibernateUtil.getSessionFactory().getCurrentSession(); session.beginTransaction(); List reports = session.createCriteria(Report.class).list(); Hibernate.initialize(reports); session.getTransaction().commit(); return reports; } Which is employed on the web page using this display html;
Note: That I added report.searchRule.name to test if I could get at the objects within the report object. It displays fine. And in my applicationContext.xml; classpath:hibernate.cfg.xml ${hibernate.dialect} Here are the ORM mappings, just in case; The hibernate.cfg.xml (as requested) com.microsoft.sqlserver.jdbc.SQLServerDriver jdbc:sqlserver:// thread false From report.hbm.xml [1]: https://stackoverflow.com/questions/345705/hibernate-lazyinitializationexception-could-not-initialize-proxy

以上就是Why am I getting a Hibernate LazyInitializationException in this Spring MVC web application when the data displays correctly?的详细内容,更多请关注web前端其它相关文章!

赞(0) 打赏
未经允许不得转载:web前端首页 » JavaScript 答疑

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

前端开发相关广告投放 更专业 更精准

联系我们

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏