为什么当数据显示正确时,在此Spring MVC Web应用程序中获取Hibernate LazyInitializationException? [英] Why am I getting a Hibernate LazyInitializationException in this Spring MVC web application when the data displays correctly?

查看:77
本文介绍了为什么当数据显示正确时,在此Spring MVC Web应用程序中获取Hibernate LazyInitializationException?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用Spring MVC创建一个Web应用程序,Hibernate作为它的ORM层。但是,由于我对这两个框架都没有经验,所以我很挣扎。



以下代码将正确显示我正在查找的所有记录,但仍然会向我的堆栈中追踪日志。我在查找有关集成Hibernate和SpringMVC的完整文档时遇到了困难(我查看了springsource.org并阅读了interweb上的各种文章)。任何人都可以指出我可能在这里做错了什么?



请注意,我花了一些试图在互联网上寻找答案,包括看<一个href =https://stackoverflow.com/questions/345705/hibernate-lazyinitializationexception-could-not-initialize-proxy>这个问题。这不幸的是没有帮助。



我还应该注意,这个应用程序的ORM部分已经在独立的Java应用程序中使用和测试过了,没有任何问题。所以我相信Spring MVC和Hibernate的集成是造成这个问题的原因。



下面是堆栈跟踪(截断)与着名的懒惰初始化问题;

  2009-03-10 12:14:50,353 [http-8084-6] ERROR org.hibernate.LazyInitializationException。< init>(LazyInitializationException。 java:19) - 无法初始化代理 - 没有会话
org.hibernate.LazyInitializationException:无法初始化代理 - 没有会话
在org.hibernate.proxy.AbstractLazyInitializer.initialize(AbstractLazyInitializer.java:57)在org.hibernate.proxy.AbstractLazyInitializer.getImplementation(AbstractLazyInitializer.java:111)

在org.hibernate.proxy.pojo.cglib.CGLIBLazyInitializer.invoke(CGLIBLazyInitializer.java:150)
at com.generic.orm.generated.SearchRule $$ EnhancerByCGLIB $$ 92abaed6.toString(< generated>)$ b $ at java.lang.String.valueOf(String.java:2827)
at java。 lang.StringBuffer.append(StringBuffer.java:21 9)
at org.apache.commons.lang.builder.ToStringStyle.appendDetail(ToStringStyle.java:578)
at org.apache.commons.lang.builder.ToStringStyle.appendInternal(ToStringStyle.java:
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)
.....

以下是我的网页控制器的代码;

  private列表与LT;报告与GT; getReports(){
Session session = HibernateUtil.getSessionFactory()。getCurrentSession();
session.beginTransaction();

列表<报告> reports = session.createCriteria(Report.class).list();
Hibernate.initialize(reports);

session.getTransaction()。commit();
返回报告;
}

使用此显示html的网页所采用的内容;

 < table border =1> 
< c:forEach items =$ {model.reports}var =report>
< tr>
< td>< c:out value =$ {report.id}/>< / td>
< td>< c:out value =$ {report.username}/>< / td>
< td>< c:out value =$ {report.thresholdMet}/>< / td>
< td>< c:out value =$ {report.results}/>< / td>
< td>< c:out value =$ {report.searchRule.name}/>< / td>
< td>< c:out value =$ {report.uuid}/>< / td>
< / tr>
< / c:forEach>
< / table>

注意:我添加了report.searchRule.name来测试我是否可以获取报告对象。它显示正常。



并在我的applicationContext.xml中;

  < bean id =sessionFactoryclass =org.springframework.orm.hibernate3.LocalSessionFactoryBean> 
< property name =configLocation>
< value> classpath:hibernate.cfg.xml< / value>
< / property>
< property name =hibernateProperties>
<道具>
< prop key =hibernate.dialect> $ {hibernate.dialect}< / prop>
< /道具>
< / property>
< / bean>

这里是ORM映射,以防万一;

hibernate.cfg.xml(根据请求)

 < hibernate-configuration> 
< session-factory>
< property name =hibernate.connection.driver_class> com.microsoft.sqlserver.jdbc.SQLServerDriver< / property>
< property name =hibernate.connection.url> jdbc:sqlserver://< removed>< / property>
< property name =hibernate.connection.username>< removed>< / property>
< property name =hibernate.connection.password>< removed>< / property>
< property name =hibernate.current_session_context_class>线程< / property>
< property name =hibernate.show_sql> false< / property>
< mapping resource =com / generic / orm / generated / Report.hbm.xml/>
< mapping resource =com / generic / orm / generated / FieldRule.hbm.xml/>
< mapping resource =com / generic / orm / generated / Reconciliation.hbm.xml/>
< mapping resource =com / generic / orm / generated / SearchRule.hbm.xml/>
< mapping resource =com / generic / orm / generated / IndexTemplate.hbm.xml/>
< mapping resource =com / generic / orm / generated / Field.hbm.xml/>
< mapping resource =com / generic / orm / generated / ErrorCode.hbm.xml/>
< / session-factory>
< / hibernate-configuration>

来自report.hbm.xml

 <休眠映射> 
< class name =com.generic.orm.generated.Reporttable =Reportschema =dbocatalog =CoolRecon>
< id name =idtype =int>
< column name =ID/>
< generator class =native/>
< / id>
< timestamp name =timeStampcolumn =TimeStamp/>
< column name =SearchRuleNamelength =50not-null =true/>
< /多对一>
< column name =ErrorCodeId/>
< /多对一>
< column name =ReconciliationNamelength =100/>
< /多对一>
< property name =usernametype =string>
< column name =Usernamelength =50/>
< / property>
< property name =supersheetDatetype =timestamp>
< column name =SupersheetDatelength =23not-null =true/>
< / property>
< property name =milliSecondsTakentype =long>
< column name =MilliSecondsTakennot-null =true/>
< / property>
< property name =thresholdMettype =boolean>
< column name =ThresholdMetnot-null =true/>
< / property>
< property name =resultstype =int>
< column name =Resultsnot-null =true/>
< / property>
< property name =exceptiontype =string>
< column name =Exceptionlength =750/>
< / property>
< property name =uuidtype =string>
< column name =UUIDlength =36not-null =true/>
< / property>
< / class>
< / hibernate-mapping>


解决方案

在SearchRule上调用了toString。 SearchRule是否有可能尚未加载的任何子对象?如果SearchRule.toString试图获取可能导致LazyInitializationException的未初始化子对象的值。


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 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.<init>(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(<generated>)
    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<Report> getReports() {
    Session session = HibernateUtil.getSessionFactory().getCurrentSession();
    session.beginTransaction();

    List<Report> 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;

<table border="1">
    <c:forEach items="${model.reports}" var="report">
        <tr>
            <td><c:out value="${report.id}"/></td>
            <td><c:out value="${report.username}"/></td>
            <td><c:out value="${report.thresholdMet}"/></td>
            <td><c:out value="${report.results}"/></td>
            <td><c:out value="${report.searchRule.name}"/></td>
            <td><c:out value="${report.uuid}"/></td>
        </tr>
    </c:forEach>
</table>

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;

<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
    <property name="configLocation">
        <value>classpath:hibernate.cfg.xml</value>
    </property>
    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">${hibernate.dialect}</prop>
        </props>
    </property>
</bean>

Here are the ORM mappings, just in case;

The hibernate.cfg.xml (as requested)

<hibernate-configuration>
  <session-factory>
    <property name="hibernate.connection.driver_class">com.microsoft.sqlserver.jdbc.SQLServerDriver</property>
    <property name="hibernate.connection.url">jdbc:sqlserver://<removed></property>
    <property name="hibernate.connection.username"><removed></property>
    <property name="hibernate.connection.password"><removed></property>
    <property name="hibernate.current_session_context_class">thread</property>
    <property name="hibernate.show_sql">false</property>
    <mapping resource="com/generic/orm/generated/Report.hbm.xml"/>
    <mapping resource="com/generic/orm/generated/FieldRule.hbm.xml"/>
    <mapping resource="com/generic/orm/generated/Reconciliation.hbm.xml"/>
    <mapping resource="com/generic/orm/generated/SearchRule.hbm.xml"/>
    <mapping resource="com/generic/orm/generated/IndexTemplate.hbm.xml"/>
    <mapping resource="com/generic/orm/generated/Field.hbm.xml"/>
    <mapping resource="com/generic/orm/generated/ErrorCode.hbm.xml"/>
  </session-factory>
</hibernate-configuration>

From report.hbm.xml

<hibernate-mapping>
    <class name="com.generic.orm.generated.Report" table="Report" schema="dbo" catalog="CoolRecon">
        <id name="id" type="int">
            <column name="ID" />
            <generator class="native" />
        </id>
        <timestamp name="timeStamp" column="TimeStamp" />
        <many-to-one name="searchRule" class="com.generic.orm.generated.SearchRule" fetch="select">
            <column name="SearchRuleName" length="50" not-null="true" />
        </many-to-one>
        <many-to-one name="errorCode" class="com.generic.orm.generated.ErrorCode" fetch="select">
            <column name="ErrorCodeId" />
        </many-to-one>
        <many-to-one name="reconciliation" class="com.generic.orm.generated.Reconciliation" fetch="select">
            <column name="ReconciliationName" length="100" />
        </many-to-one>
        <property name="username" type="string">
            <column name="Username" length="50" />
        </property>
        <property name="supersheetDate" type="timestamp">
            <column name="SupersheetDate" length="23" not-null="true" />
        </property>
        <property name="milliSecondsTaken" type="long">
            <column name="MilliSecondsTaken" not-null="true" />
        </property>
        <property name="thresholdMet" type="boolean">
            <column name="ThresholdMet" not-null="true" />
        </property>
        <property name="results" type="int">
            <column name="Results" not-null="true" />
        </property>
        <property name="exception" type="string">
            <column name="Exception" length="750" />
        </property>
        <property name="uuid" type="string">
            <column name="UUID" length="36" not-null="true" />
        </property>
    </class>
</hibernate-mapping>

解决方案

I am just guessing but from the stack trace it seems that toString is being called on SearchRule. Does SearchRule have any child objects that may not have been loaded? If SearchRule.toString was trying to get the value for an uninitialised child object that could result in the LazyInitializationException.

这篇关于为什么当数据显示正确时,在此Spring MVC Web应用程序中获取Hibernate LazyInitializationException?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆