Announcement Announcement Module
Collapse
No announcement yet.
Hibernate LazyInitializationException - change from Spring 3.0.0 to 3.0.1? Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Hibernate LazyInitializationException - change from Spring 3.0.0 to 3.0.1?

    I'm a bit green with both Spring and Hibernate, so please bear with me.

    We have recently taken over a web app that makes extensive use of Spring and Hibernate, and we have to make a number of changes. As we received it, it was setup with Spring 3.0.0.GA and Hibernate 3.3.1.GA. We need to utilize mvc:resources for static content, so we need to be using at least Spring 3.0.4. Unfortunately, some other code breaks; through trial and error I've found that the offending code works in 3.0.0 but breaks in 3.0.1:

    Code:
    org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: com.iir.sort.model.Offender.offenderAliases, no session or session was closed
    	at org.hibernate.collection.AbstractPersistentCollection.throwLazyInitializationException(AbstractPersistentCollection.java:380)
    	at org.hibernate.collection.AbstractPersistentCollection.throwLazyInitializationExceptionIfNotConnected(AbstractPersistentCollection.java:372)
    	at org.hibernate.collection.AbstractPersistentCollection.initialize(AbstractPersistentCollection.java:365)
    	at org.hibernate.collection.AbstractPersistentCollection.read(AbstractPersistentCollection.java:108)
    	at org.hibernate.collection.PersistentSet.iterator(PersistentSet.java:186)
    	at com.iir.sort.model.Offender.getFilteredOffenderAliases(Offender.java:822)
    I've seen tons of references to this error, but I'm perplexed because it works fine in 3.0.0. Is there a known change post-3.0.0 that would affect this? FYI, I've also tried 3.0.4.GA, 3.1.0.RC1, 3.1.0.GA and 3.1.2.GA; I'm having the same issue in all of those. The *only* changes are to the JARs.

    I'm not sure where to start, but here is some of the relevant code:

    Offender.java, lines 811-832:
    Code:
    	@OneToMany(mappedBy = "offender", fetch = FetchType.LAZY, cascade = CascadeType.ALL)
    	public Set<OffenderAliases> getOffenderAliases() {
    		if (offenderAliases == null) {
    			offenderAliases = new HashSet<OffenderAliases>();
    		}
    		return offenderAliases;
    	}
    
    	@Transient
    	public Set<OffenderAliases> getFilteredOffenderAliases() {
    		Set<OffenderAliases> returnSet = new HashSet<OffenderAliases>();
    		for (OffenderAliases item : getOffenderAliases()) {
    			if (item.getEntityChangeSet() != null && item.getEntityChangeSet().getId() != null) {
    				if (item.getEntityChangeSet().getEntityChangeType().getId() != EntityChangeType.DELETE) {
    					returnSet.add(item);
    				}
    			} else {
    				returnSet.add(item);
    			}
    		}
    		return returnSet;
    	}
    The fact that this works just fine with Spring 3.0.0 has me scratching my head. Any ideas?

  • #2
    Any ideas?

    Some other threads suggested changing FetchType.LAZY to FetchType.EAGER to see what happens. This does seem to work, but I'm a bit perplexed about why FetchType.LAZY would work with Spring 3.0.0 but not 3.0.1.

    Comment


    • #3
      Bumping this. Some more details:

      Code:
      	<context:component-scan base-package="com.iir.sort" />
      	
      	<bean id="exporter"
      		class="org.springframework.jmx.export.MBeanExporter"
      		lazy-init="false">
      		<property name="autodetect" value="true"></property>
      		<property name="namingStrategy" ref="namingStrategy"></property>
      		<property name="assembler" ref="assembler"></property>
      	</bean>
      	<bean id="attributeSource"
      		class="org.springframework.jmx.export.annotation.AnnotationJmxAttributeSource" />
      	<bean id="assembler"
      		class="org.springframework.jmx.export.assembler.MetadataMBeanInfoAssembler">
      		<property name="attributeSource" ref="attributeSource" />
      	</bean>
      	<bean id="namingStrategy"
      		class="org.springframework.jmx.export.naming.MetadataNamingStrategy">
      		<property name="attributeSource" ref="attributeSource" />
      	</bean>
      	
      	<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
          	<property name="basenames">
            		<list>
              		<value>messages</value>
            		</list>
          	</property>
        	</bean>
      
      	<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
      	    <property name="jndiName">
      	        <value>java:comp/env/jdbc/SRTORA</value>
      	    </property>
      	</bean>
      
      	<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
      		<property name="dataSource" ref="dataSource" />
      		<property name="persistenceUnitName" value="sortPU" />
      		<property name="jpaVendorAdapter">
      			<bean
      				class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
      				<property name="showSql" value="false" />
      				<property name="generateDdl" value="false" />
      				<property name="databasePlatform"
      					value="org.hibernate.dialect.Oracle10gDialect" />
      			</bean>
      		</property>
      	</bean>
      	
      	<bean id="transactionManager"
      		class="org.springframework.orm.jpa.JpaTransactionManager">
      		<property name="entityManagerFactory" ref="entityManagerFactory" />
      	</bean>
      
      	<tx:annotation-driven transaction-manager="transactionManager" />
      	
      	<bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor" />
      I've seen this suggested for web.xml:

      Code:
      	<filter>
              <filter-name>openEntityManagerInViewFilter</filter-name>
              <filter-class>org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter</filter-class>
              <init-param>
      	        <param-name>entityManagerFactoryBeanName</param-name>
      	        <param-value>entityManagerFactory</param-value>
      	</init-param>
          </filter>
      
          <filter-mapping>
              <filter-name>openEntityManagerInViewFilter</filter-name>
              <url-pattern>/*</url-pattern>
          </filter-mapping>
      I'm able to get farther, but subsequently run into this problem:

      Code:
      org.hibernate.event.def.AbstractFlushingEventListener - Could not synchronize database state with session
      Anyone? I'm still perplexed as to why this all worked fine with Spring 3.0.0 but breaks with anything newer.

      Comment


      • #4
        One further note:

        Originally posted by lukpac View Post
        I've seen this suggested for web.xml:

        Code:
        	<filter>
                <filter-name>openEntityManagerInViewFilter</filter-name>
                <filter-class>org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter</filter-class>
                <init-param>
        	        <param-name>entityManagerFactoryBeanName</param-name>
        	        <param-value>entityManagerFactory</param-value>
        	</init-param>
            </filter>
        
            <filter-mapping>
                <filter-name>openEntityManagerInViewFilter</filter-name>
                <url-pattern>/*</url-pattern>
            </filter-mapping>
        I'm able to get farther, but subsequently run into this problem:

        Code:
        org.hibernate.event.def.AbstractFlushingEventListener - Could not synchronize database state with session
        I get the same error when attempting to use the filter with 3.0.0. To summarize:

        3.0.0 with no filter - application works correctly
        3.0.0 with OpenEntityManagerInViewFilter - "Could not synchronize database state with session" error
        3.0.4 with no filter - LazyInitializationException
        3.0.4 with OpenEntityManagerInViewFilter - "Could not synchronize database state with session" error

        The "Could not synchronize..." error appears to be stemming from a JDBC template call. Removing that allows one to get a bit further, but attempting an update results in this error:

        Code:
        2012-10-03 12:28:04,614 [http-bio-80-exec-3] ERROR org.hibernate.LazyInitializationException - could not initialize proxy - no Session
        org.hibernate.LazyInitializationException: could not initialize proxy - no Session
        	at org.hibernate.proxy.AbstractLazyInitializer.initialize(AbstractLazyInitializer.java:86)
        	at org.hibernate.proxy.pojo.BasicLazyInitializer.invoke(BasicLazyInitializer.java:99)
        	at org.hibernate.proxy.pojo.javassist.JavassistLazyInitializer.invoke(JavassistLazyInitializer.java:184)
        	at com.iir.sort.model.LookupValues_$$_javassist_70.setId(LookupValues_$$_javassist_70.java)
        Again, note that with 3.0.0 (and *without* OpenEntityManagerInViewFilter) this works fine.

        Any help would be appreciated.

        Comment

        Working...
        X