Java – 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 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>( - could not initialize proxy - no Session
org.hibernate.LazyInitializationException: could not initialize proxy - no Session
    at org.hibernate.proxy.AbstractLazyInitializer.initialize(
    at org.hibernate.proxy.AbstractLazyInitializer.getImplementation(
    at org.hibernate.proxy.pojo.cglib.CGLIBLazyInitializer.invoke(
    at com.generic.orm.generated.SearchRule$$EnhancerByCGLIB$$92abaed6.toString(<generated>)
    at java.lang.String.valueOf(
    at java.lang.StringBuffer.append(
    at org.apache.commons.lang.builder.ToStringStyle.appendDetail(
    at org.apache.commons.lang.builder.ToStringStyle.appendInternal(
    at org.apache.commons.lang.builder.ToStringStyle.append(
    at org.apache.commons.lang.builder.ToStringBuilder.append(
    at org.apache.commons.lang.builder.ReflectionToStringBuilder.appendFieldsIn(

Here is a code from my web page controller;

private List<Report> getReports() {
    Session session = HibernateUtil.getSessionFactory().getCurrentSession();

    List<Report> reports = session.createCriteria(Report.class).list();

    return reports;

Which is employed on the web page using this display html;

<table border="1">
    <c:forEach items="${model.reports}" var="report">
            <td><c:out value="${}"/></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="${}"/></td>
            <td><c:out value="${report.uuid}"/></td>

Note: That I added 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">
    <property name="hibernateProperties">
            <prop key="hibernate.dialect">${hibernate.dialect}</prop>

Here are the ORM mappings, just in case;

The hibernate.cfg.xml (as requested)

    <property name="hibernate.connection.driver_class"></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"/>

From report.hbm.xml

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

Best Solution

I just went through this LazyInitialization marathon.

The core problem is that you're trying to access an hibernate-managed entity outside of the lifecycle of the Session, i.e. in the web view of Spring MVC. In my case, this was a List<> @OneToMany association, which are lazily loaded by default.

There are a few different approaches -- Mark mentioned one, where you do a "dummy" iteration over the lazy associations. You can also force eager loading, either via the configuration (class-wide) (in JPA it'd be @Fetch(value = FetchType.EAGER)) or more specifically through the HQL. But this will prove more problematic if your lazy associations are Lists.

The cleanest solution I found was to use Spring's OpenEntityManagerInViewFilter (there's an OpenSessionInViewFilter for Hibernate) -- a simple servlet filter you drop in to web.xml (ahead of your other servlet filters), and Spring will automatically create a thread-safe, transaction-aware Session per-HTTP-request. No more LazyInitializationException!

Related Question