开发者

Lazy Loading error

I know that this issue was already raised here several time. However I couldn't find any solution which will help so I decided to post it again.

I'm using Hibernate with Spring MVC framework and I'm trying to fetch my class wth sub class:

public class Scene implements Serializable{
private Long id;
private Author author;
//omitting getters and setters
} 

public class Author{
private Long id;
Private String  name; 
//omitting getters and setters
}

My Hibernate configuration is the following:

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
  <property name="driverClassName" value="org.postgresql.Driver" />
  <property name="url" value="jdbc:postgresql://127.0.0.1/doolloop2" />
  <property name="username" value="doolloop2" />
  <property name="password" value="doolloop" />
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
  <property name="dataSource" ref="dataSource" />
     <property name="mappingLocations">
      <list>
        <value>WEB-INF/mapping/Scene.hbm.xml</value>
        <value>WEB-INF/mapping/Author.hbm.xml</value>
      </list>
    </property>
    <property name="hibernateProperties">
 <props>
  <prop key="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</prop> 
  <prop key="hibernate.show_sql">true</prop> 
  <prop key="hibernate.hbm2ddl.auto">update</prop> 
  <prop key="hibernate.cache.use_second_level_cache">true</prop> 
  <prop key="hibernate.cache.use_query_cache">true</prop> 
  <prop key="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</prop>
  </props>
  </property>
</bean>
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
  <property name="sessionFactory" ref="sessionFactory" />
</bean>

<bean id="txService" class="com.doolloop.services.TxDataBaseServiceImpl">
        <property name="sessionFactory">
        <ref local="sessionFactory"/>
        </property>
    </bean> 

Now I have methods which gets my scene:

@Override
    @Transactional(readOnly = true)
    public Scene getScene(Long id) {
        Session session = this.sessionFactory.getCurrentSession();
        Query q = session.createQuery("select scene from com.doolloop.objects.Scene scene where scene.id=:id");
        q.setLong("id", id);
        Scene scene = (Scene)q.uniqueResult()
        return scene;

    }

and my spring mvc code:

 @RequestMapping(value="/getSceneMetaData.dlp",method = RequestMethod.GET)
     @ResponseBody
     public Scene getSceneMetaData(@RequestParam String id){
        Scene scene = service.getScene(Long.parseLong(id));
        return scene;
    }

When I'm executing it i got the followng exception:

Dec 5, 2010 12:29:21 PM org.hibernate.LazyInitializationException <init>
    SEVERE: failed to lazily initialize a collection of role: com.doolloop.objects.Scene.author, no session or session was closed
    org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: com.doolloop.objects.Scene.vauthor, no session or session was closed

I googled it and understand that it happens because it tries to fetch author when Transaction is closed and that author class is lazy by default, but what I can't understand is how should I modify my code n order to solve it.

Any suggestion s more the welcomed.

UPD: here is the mapping files for both classes:

<class
        name="com.doolloop.objects.Scene"
        table="scene"
    >
        <id
            name="id"
            column="Id"
            type="java.lang.Long"
            unsaved-value="null"
        >
         <generator class="sequence">
                <param name="sequence">doolloop2.sceneseq</param>
            </generator>
        </id>
    <many-to-one
         name="author"
         class="com.doolloop.objects.Author"
         cascade="all"
         column="authorid"
         unique="false"
     />  
    </class>

<hibernate-mapping>
    <class
        name="com.doolloop.objects.Author"
        table="authors"
    >
        <id
            name="id"
            column="Id"
            type="java.lang.Long"
            unsaved-value="null"
        >
         <generator class="sequence">
                <param name="sequence">doolloop2.authorseq</param>
            </generator>
        </id>
        <property
            name="name"
            update="true"
            insert="true"
            not-null="false"
            unique="false"
            type="java.lang.String"
        >
            <column name="name" />
        </property>
             <property
            name="email"
            update="true"
            insert="true"
            not-null="false"
            unique="false"
            type="java.lang.String"
        >
            <column name="email" />
        </property>
        <property
            name="site"
            update="true"
            insert="true"
            not-null="false"
            unique="false"
            type="java.lang.String"
        >
            <column name="site" />
        </property>
       <property
            name="contactInfo"
            update="true"
            insert="true"
            not-null="false"
            unique="false"
            type="java.lang.String"
        >
            <column name="contactInfo" />
        </property>
    </class>

UPD2 I added the following to my web.xml:

 <listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>WEB-INF/applicationContext.xml</param-value>
</context-param>
  <filter>
    <filter-name>openSessionInViewFilter</filter-name>
    <filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
</filter>
<filter-mapping>
    <filter-name>openSessionInViewFilter</filter-name>
    <url-pattern>/getSceneMetaData.dlp</url-pattern>
</filter-mapping>

it gave me the following exception:

org.codehaus.jackson.map.JsonMappingException: could not initialize proxy - no Session (through reference chain: com.doolloop.objects.Scene["author"]->com.doolloop.objects.Author_$$_javassist_4["name"])

Update3:

This is the log4j output for this operation:

2010-12-06 11:48:09,031 DEBUG [org.springframework.orm.hibernate3.support.OpenSessionInViewFilter] - Using SessionFactory 'sessionFactory' for OpenSessionInViewFilter
2010-12-06 11:48:09,036 DEBUG [org.springframework.beans.factory.support.DefaultListableBeanFactory] - Returning cached instance of singleton bean 'sessionFactory'
2010-12-06 11:48:09,040 DEBUG [org.springframework.orm.hibernate3.support.OpenSessionInViewFilter] - Opening single Hibernate Session in OpenSessionInViewFilter
2010-12-06 11:48:09,042 DEBUG [org.springframework.orm.hibernate3.SessionFactoryUtils] - Opening Hibernate Session
2010-12-06 11:48:09,048 DEBUG [org.springframework.web.servlet.DispatcherServlet] - DispatcherServlet with name 'doolloop' processing GET request for [/dlp/getSceneMetaData.dlp]
2010-12-06 11:48:09,056 DEBUG [org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping] - Mapping [/getSceneMetaData.dlp] to HandlerExecutionChain with handler [com.doolloop.controllers.SceneController@21c33b55] and 2 interceptors
2010-12-06 11:48:09,060 DEBUG [org.springframework.web.servlet.DispatcherServlet] - Last-Modified value for [/dlp/getSceneMetaData.dlp] is: -1
2010-12-06 11:48:09,142 DEBUG [org.springframework.core.convert.support.GenericConversionService] - Converting value '140' of [TypeDescriptor java.lang.String] to [TypeDescriptor @org.springframework.web.bind.annotation.RequestParam java.lang.String]
2010-12-06 11:48:09,146 DEBUG [org.springframework.core.convert.support.GenericConversionService] - Converted to '140'
2010-12-06 11:48:09,148 DEBUG [org.springframework.web.bind.annotation.support.HandlerMethodInvoker] - Invoking request handler method: public com.doolloop.objects.Scene com.doolloop.controllers.SceneController.getSceneMetaData(java.lang.String)
2010-12-06 11:48:16,401 DEBUG [org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerExceptionResolver] - Resolving exception from handler [com.doolloop.controllers.SceneController@21c33b55]: org.codehaus.jackson.map.JsonMappingException: could not initialize proxy - no Session (through reference chain: com.doolloop.objects.Scene["author"]->com.doolloop.objects.Author_$$_javassist_4["name"])
2010-12-06 11:48:16,561 DEBUG [org.springframework.web.servlet.mvc.annotation.ResponseStatusExceptionResolver] - Resolving exception from handler [com.doolloop.controllers.SceneController@21c33b55]: org.codehaus.jackson.map.JsonMappingException: could not initialize proxy - no Session (through reference chain: com.doolloop.objects.Scene["author"]->com.doolloop.objects.Author_$$_javassist_4["name"])
2010-12-06 11:48:16,568 DEBUG [org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver] - Resolving exception from handler [com.doolloop.controllers.SceneController@21c33b55]: org.codehaus.jackson.map.JsonMappingException: could not initialize proxy - no Session (through reference chain: com开发者_JS百科.doolloop.objects.Scene["author"]->com.doolloop.objects.Author_$$_javassist_4["name"])
2010-12-06 11:48:16,572 DEBUG [org.springframework.web.servlet.DispatcherServlet] - Could not complete request
org.codehaus.jackson.map.JsonMappingException: could not initialize proxy - no Session (through reference chain: com.doolloop.objects.Scene["author"]->com.doolloop.objects.Author_$$_javassist_4["name"])
    at org.codehaus.jackson.map.JsonMappingException.wrapWithPath(JsonMappingException.java:215)

//// Entire stack trace goes here.

2010-12-06 11:48:16,660 DEBUG [org.springframework.orm.hibernate3.support.OpenSessionInViewFilter] - Closing single Hibernate Session in OpenSessionInViewFilter
2010-12-06 11:48:16,662 DEBUG [org.springframework.orm.hibernate3.SessionFactoryUtils] - Closing Hibernate Session

Update4:

I see only one select n console:

Hibernate: select scene0_.Id as Id20_0_, scene0_.name as name20_0_, scene0_.description as descript3_20_0_, scene0_.defaultView as defaultV4_20_0_, scene0_.authorid as authorid20_0_ from scene scene0_ where scene0_.Id=?

However if I modify my getScene method that way:

 public Scene getScene(Long id) {
        Session session = this.sessionFactory.getCurrentSession();
        Query q = session.createQuery("select scene from com.doolloop.objects.Scene scene where scene.id=:id");
        q.setLong("id", id);
        Scene scene = (Scene)q.uniqueResult()
        **Author author = scene.getAuthor();**
        return scene; 

I will see second select appears:

Hibernate: select author0_.Id as Id23_0_, author0_.name as name23_0_, author0_.email as email23_0_, author0_.site as site23_0_, author0_.contactInfo as contactI5_23_0_ from authors author0_ where author0_.Id=?

but author object wll be still empty.

calling the same getter from controller causes exception.


It's probably worth posting Scene.hbm.xml and Author.hbm.xml first..

From what I've seen here: https://forum.hibernate.org/viewtopic.php?f=1&t=994772&start=0 Many-to-One relationships are loaded EAGERLY by default if you use annotations, however that's not what happens if you use XML mappings!!

I'd say modify the Many-to-One relationship and explicitly set fetch="EAGER".

So the result should be:

<many-to-one
     name="author"
     class="com.doolloop.objects.Author"
     cascade="all"
     column="authorid"
     unique="false"
     fetch="EAGER"
 />  

You've mentioned that you have 2 usage scenarios: one where you fetch the Scene object and you need all its dependencies and one where you're only interested in the id and a few fields for it. This seems to be a matter of defining the Hibernate Session boundaries. You can use the SessionInViewFilter described here: http://community.jboss.org/wiki/OpenSessioninView This way, the Hibernate session will be open for the duration of a http request, so you will be able to access lazy loaded references right to render time. Does this help?

=================================
I've seen your OpenSessionInViewFilter definition - it maybe worth explicitly setting the name of your HibernateSessionFactory:

<filter>
   <filter-name>openSessionInViewFilter</filter-name>
   <filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
     <init-param>
        <param-name>sessionFactoryBeanName</param-name>
        <param-value>sessionFactory</param-value>
     </init-param>
 </filter>

Also, I don't see a proper initialization of your Tx Manager in your Spring Config. Just defining it is not enough, you need:

<tx:annotation-driven transaction-manager="transactionManager"/>

=================================
From what I've seen in Hibernate documentation, it is very likely that once you close the transaction, your Hibernate session will end (even if you use OpenSessionInView Filter). Have a look here: http://community.jboss.org/wiki/OpenSessioninView#Can_I_commit_the_transaction_before_rendering_the_view

Once you're closing your transaction, the db connection is returned to the pool and all further access to the DB will require a new connection, which should have the auto-commit mode on.

However, in your case, I think the solution would be to make sure you've loaded all you need to load from the DB while the transaction is still open!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜