1

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.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.

1 Answer 1

7

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!

Sign up to request clarification or add additional context in comments.

11 Comments

Done, But I don't think the problem s there, the problem s in the way I handle transaction, but anyway thank you in advance.
First of all thank you for this response I did not know that before that. However, would like to keep it Lazy, because I have two cases one when retrieve scene and I need all items and second when I search and I need there only ID, name and description of Scene object.
unfortunately, it hasn't help :(
So, you're using OpenSessionInViewFilter, you've configured TxManager properly, and you still have this problem.. Is the data that your are working on fetched during that particular request or do you get it from the http Session. In other words, are you sure the objects that your use are still attached to the Hibernate session?
I think that Yes. please looks at the log4j for this operation. In my update 3.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.