Results 1 to 3 of 3

Thread: Lazy loading not working for session attributes

  1. #1
    Join Date
    Apr 2008
    Location
    Singapore
    Posts
    83

    Default Lazy loading not working for session attributes

    Hi!

    I have a object(Nomination) in session. When I try to access from the view I'm getting an exception :

    org.hibernate.LazyInitializationException: could not initialize proxy - no Session

    if the Nomination object was passed as request attribute there is no problem. I dont wont to do that bcos i want to show Nomination details in all my jsp pages like a header

    what I want to do is put the Nomination in session and access it from all of my jsp pages.


    Nomination.hbm.xml
    Code:
    <hibernate-mapping package="org.sya.domain">
    
    	<class name="Nomination" table="nominations">
    	
    		<id name="nominationId" column="nomination_id" type="integer">
    			<generator class="increment" />
    		</id>
    
    		<many-to-one name="nominee" class="Person" column="person_id"/>		
    		<many-to-one name="nomineeType" class="NomineeType" column="nominee_type_id" />
    		<many-to-one name="category" class="Category" column="category_id" />
    		<property name="registeredDate" column="registered_date" type="timestamp"/>
    		
    	</class>
    data-access-config
    Code:
    	<bean id="hibernateTransactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
    		<property name="sessionFactory" ref="sessionFactory" />
    	</bean>
    
    	<bean id="baseTransactionProxy" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean" abstract="true">
    		<property name="transactionManager" ref="hibernateTransactionManager" />
    		<property name="transactionAttributes">
    			<props>
    				<prop key="insert*">PROPAGATION_REQUIRED</prop>
    				<prop key="update*">PROPAGATION_REQUIRED</prop>
    				<prop key="delete*">PROPAGATION_REQUIRED</prop>				
    				<prop key="*">PROPAGATION_SUPPORTS,readOnly</prop>
    			</props>
    		</property>
    	</bean>
    Code:
    	<bean id="openSessionInViewInterceptor" class="org.springframework.orm.hibernate3.support.OpenSessionInViewInterceptor">
    		<property name="sessionFactory" ref="sessionFactory" />
    		<property name="singleSession" value="true" />
    		<property name="flushModeName" value="FLUSH_NEVER" />
    	</bean>
    
    	<bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
    		<property name="alwaysUseFullPath" value="true" />
    		<property name="interceptors">
    			<list>
    				<ref bean="openSessionInViewInterceptor" />
    			</list>
    		</property>
    		<property name="mappings">
    			<props>
    				<prop key="/public/index">indexController</prop>
    				<prop key="/public/login">loginController</prop>
    				<prop key="/public/home">userRedirectController</prop>
    				<prop key="/nominee/register">nomineeRegistrationController</prop>
    				<prop key="/nominee/registernomination">nominationRegistrationController</prop>
    				<prop key="/nominee/switchnomination">switchNominationController</prop>
    				<prop key="/nominee/home">nomineeHomeController</prop>
    			</props>
    		</property>
    	</bean>
    view.jsp
    Code:
    <c:set var="nomination" value="${sessionScope.NOMINATION}"/>
    
    <c:if test="${nomination ne null}">
    	<c:set var="nomineeType" value="${nomination.nomineeType}"/>
    	<c:set var="category" value="${nomination.category}"/>
      
            Category : ${category.categoryName}
    </c:if>
    does any one know the reason for it to behave like this. is there any solution (without lazy="false" in my Nomination.hbm) ?

    thanks in advance
    Last edited by rukshan; Sep 30th, 2008 at 02:48 AM.

  2. #2
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,624

    Default

    It is never going to work, it only is going to work for the initial request that puts the object in the session. The hibernate session will remain open only for that request. As soon as you move to another page the session to which your object belongs is already closed.

    Either put it in the session again for each request (which makes it pretty useless so you want probably to put it at the request) or make it non lazy/i.e eagerly retrieve all the data needed.
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  3. #3
    Join Date
    Apr 2008
    Location
    Singapore
    Posts
    83

    Default

    I'll go with 2nd option (eagerly retrieve data)

    Thank you for the advice

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •