Announcement Announcement Module
Collapse
No announcement yet.
hibernate lazy init error Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • hibernate lazy init error

    im studying spring framework and a i got this error when i set lazy="true" on the Cidade object mapping xml. If i set lazy="false" the application works great, but it makes a query for each Cidade object in the Cliente object

    Stack Trace
    Code:
    20/10/2004 11:23:42 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
    INFO: Loading XML bean definitions from file [C:\Henrique\programacao\Locadora\war\WEB-INF\applicationContext.xml]
    20/10/2004 11:23:42 org.springframework.context.support.AbstractXmlApplicationContext refreshBeanFactory
    INFO: Bean factory for application context [org.springframework.context.support.FileSystemXmlApplicationContext;hashCode=8880493]: org.springframework.beans.factory.support.DefaultListableBeanFactory defining beans [dataSource,sessionFactory,transactionManager,clienteDAO,cidadeDAO,clienteManagerTarget,cidadeManagerTarget,clienteManager,cidadeManager]; Root of BeanFactory hierarchy
    20/10/2004 11:23:42 org.springframework.context.support.AbstractApplicationContext refresh
    INFO: 9 beans defined in ApplicationContext [org.springframework.context.support.FileSystemXmlApplicationContext;hashCode=8880493]
    20/10/2004 11:23:42 org.springframework.context.support.AbstractApplicationContext initMessageSource
    INFO: No MessageSource found for context [org.springframework.context.support.FileSystemXmlApplicationContext;hashCode=8880493]: using empty default
    20/10/2004 11:23:42 org.springframework.context.support.AbstractApplicationContext initApplicationEventMulticaster
    INFO: No ApplicationEventMulticaster found for context [org.springframework.context.support.FileSystemXmlApplicationContext;hashCode=8880493]: using default
    20/10/2004 11:23:42 org.springframework.context.support.AbstractApplicationContext refreshListeners
    INFO: Refreshing listeners
    20/10/2004 11:23:42 org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
    INFO: Pre-instantiating singletons in factory [org.springframework.beans.factory.support.DefaultListableBeanFactory defining beans [dataSource,sessionFactory,transactionManager,clienteDAO,cidadeDAO,clienteManagerTarget,cidadeManagerTarget,clienteManager,cidadeManager]; Root of BeanFactory hierarchy]
    20/10/2004 11:23:42 org.springframework.beans.factory.support.AbstractBeanFactory getBean
    INFO: Creating shared instance of singleton bean 'dataSource'
    20/10/2004 11:23:42 org.springframework.beans.factory.support.AbstractBeanFactory getBean
    INFO: Creating shared instance of singleton bean 'sessionFactory'
    20/10/2004 11&#58;23&#58;42 net.sf.hibernate.cfg.Environment <clinit>
    INFO&#58; Hibernate 2.1.3
    20/10/2004 11&#58;23&#58;42 net.sf.hibernate.cfg.Environment <clinit>
    INFO&#58; hibernate.properties not found
    20/10/2004 11&#58;23&#58;42 net.sf.hibernate.cfg.Environment <clinit>
    INFO&#58; using CGLIB reflection optimizer
    20/10/2004 11&#58;23&#58;43 net.sf.hibernate.cfg.Binder bindRootClass
    INFO&#58; Mapping class&#58; br.unisantos.engenharia.locadora.model.Cliente -> cliente
    20/10/2004 11&#58;23&#58;43 net.sf.hibernate.cfg.Binder bindRootClass
    INFO&#58; Mapping class&#58; br.unisantos.engenharia.locadora.model.Cidade -> cidade
    20/10/2004 11&#58;23&#58;43 org.springframework.orm.hibernate.LocalSessionFactoryBean afterPropertiesSet
    INFO&#58; Building new Hibernate SessionFactory
    20/10/2004 11&#58;23&#58;43 net.sf.hibernate.cfg.Configuration secondPassCompile
    INFO&#58; processing one-to-many association mappings
    20/10/2004 11&#58;23&#58;43 net.sf.hibernate.cfg.Binder bindCollectionSecondPass
    INFO&#58; Mapping collection&#58; br.unisantos.engenharia.locadora.model.Cidade.clientes -> cliente
    20/10/2004 11&#58;23&#58;43 net.sf.hibernate.cfg.Configuration secondPassCompile
    INFO&#58; processing one-to-one association property references
    20/10/2004 11&#58;23&#58;43 net.sf.hibernate.cfg.Configuration secondPassCompile
    INFO&#58; processing foreign key constraints
    20/10/2004 11&#58;23&#58;43 net.sf.hibernate.dialect.Dialect <init>
    INFO&#58; Using dialect&#58; net.sf.hibernate.dialect.FirebirdDialect
    20/10/2004 11&#58;23&#58;43 net.sf.hibernate.cfg.SettingsFactory buildSettings
    INFO&#58; Use outer join fetching&#58; true
    20/10/2004 11&#58;23&#58;43 net.sf.hibernate.connection.ConnectionProviderFactory newConnectionProvider
    INFO&#58; Initializing connection provider&#58; org.springframework.orm.hibernate.LocalDataSourceConnectionProvider
    20/10/2004 11&#58;23&#58;43 net.sf.hibernate.transaction.TransactionManagerLookupFactory getTransactionManagerLookup
    INFO&#58; No TransactionManagerLookup configured &#40;in JTA environment, use of process level read-write cache is not recommended&#41;
    20/10/2004 11&#58;23&#58;43 net.sf.hibernate.cfg.SettingsFactory buildSettings
    INFO&#58; Use scrollable result sets&#58; true
    20/10/2004 11&#58;23&#58;43 net.sf.hibernate.cfg.SettingsFactory buildSettings
    INFO&#58; Use JDBC3 getGeneratedKeys&#40;&#41;&#58; false
    20/10/2004 11&#58;23&#58;43 net.sf.hibernate.cfg.SettingsFactory buildSettings
    INFO&#58; Optimize cache for minimal puts&#58; false
    20/10/2004 11&#58;23&#58;43 net.sf.hibernate.cfg.SettingsFactory buildSettings
    INFO&#58; echoing all SQL to stdout
    20/10/2004 11&#58;23&#58;43 net.sf.hibernate.cfg.SettingsFactory buildSettings
    INFO&#58; Query language substitutions&#58; &#123;&#125;
    20/10/2004 11&#58;23&#58;43 net.sf.hibernate.cfg.SettingsFactory buildSettings
    INFO&#58; cache provider&#58; net.sf.ehcache.hibernate.Provider
    20/10/2004 11&#58;23&#58;43 net.sf.hibernate.cfg.Configuration configureCaches
    INFO&#58; instantiating and configuring caches
    20/10/2004 11&#58;23&#58;43 net.sf.hibernate.impl.SessionFactoryImpl <init>
    INFO&#58; building session factory
    20/10/2004 11&#58;23&#58;44 net.sf.hibernate.impl.SessionFactoryObjectFactory addInstance
    INFO&#58; no JNDI name configured
    20/10/2004 11&#58;23&#58;44 org.springframework.beans.factory.support.AbstractBeanFactory getBean
    INFO&#58; Creating shared instance of singleton bean 'transactionManager'
    20/10/2004 11&#58;23&#58;44 org.springframework.orm.hibernate.HibernateTransactionManager afterPropertiesSet
    INFO&#58; Using DataSource &#91;org.apache.commons.dbcp.BasicDataSource@1687e7c&#93; of Hibernate SessionFactory for HibernateTransactionManager
    20/10/2004 11&#58;23&#58;44 org.springframework.beans.factory.support.AbstractBeanFactory getBean
    INFO&#58; Creating shared instance of singleton bean 'clienteDAO'
    20/10/2004 11&#58;23&#58;44 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
    INFO&#58; Loading XML bean definitions from class path resource &#91;org/springframework/jdbc/support/sql-error-codes.xml&#93;
    20/10/2004 11&#58;23&#58;44 org.springframework.beans.factory.support.AbstractBeanFactory getBean
    INFO&#58; Creating shared instance of singleton bean 'DB2'
    20/10/2004 11&#58;23&#58;44 org.springframework.beans.factory.support.AbstractBeanFactory getBean
    INFO&#58; Creating shared instance of singleton bean 'HSQL'
    20/10/2004 11&#58;23&#58;44 org.springframework.beans.factory.support.AbstractBeanFactory getBean
    INFO&#58; Creating shared instance of singleton bean 'MS-SQL'
    20/10/2004 11&#58;23&#58;44 org.springframework.beans.factory.support.AbstractBeanFactory getBean
    INFO&#58; Creating shared instance of singleton bean 'MySQL'
    20/10/2004 11&#58;23&#58;44 org.springframework.beans.factory.support.AbstractBeanFactory getBean
    INFO&#58; Creating shared instance of singleton bean 'Oracle'
    20/10/2004 11&#58;23&#58;44 org.springframework.beans.factory.support.AbstractBeanFactory getBean
    INFO&#58; Creating shared instance of singleton bean 'Informix'
    20/10/2004 11&#58;23&#58;44 org.springframework.beans.factory.support.AbstractBeanFactory getBean
    INFO&#58; Creating shared instance of singleton bean 'PostgreSQL'
    20/10/2004 11&#58;23&#58;44 org.springframework.beans.factory.support.AbstractBeanFactory getBean
    INFO&#58; Creating shared instance of singleton bean 'Sybase'
    20/10/2004 11&#58;23&#58;44 org.springframework.jdbc.support.SQLErrorCodesFactory <init>
    INFO&#58; SQLErrorCodes loaded&#58; &#91;HSQL Database Engine, Oracle, Sybase SQL Server, Microsoft SQL Server, Informix Dynamic Server, PostgreSQL, DB2*, MySQL&#93;
    20/10/2004 11&#58;23&#58;44 org.springframework.jdbc.support.SQLErrorCodesFactory getErrorCodes
    INFO&#58; Looking up default SQLErrorCodes for DataSource
    20/10/2004 11&#58;23&#58;44 org.springframework.jdbc.support.SQLErrorCodesFactory getErrorCodes
    INFO&#58; Database Product Name is Firebird
    20/10/2004 11&#58;23&#58;44 org.springframework.jdbc.support.SQLErrorCodesFactory getErrorCodes
    INFO&#58; Driver Version is 1.5
    20/10/2004 11&#58;23&#58;44 org.springframework.jdbc.support.SQLErrorCodesFactory getErrorCodes
    INFO&#58; Error Codes for Firebird not found
    20/10/2004 11&#58;23&#58;44 org.springframework.beans.factory.support.AbstractBeanFactory getBean
    INFO&#58; Creating shared instance of singleton bean 'cidadeDAO'
    20/10/2004 11&#58;23&#58;44 org.springframework.jdbc.support.SQLErrorCodesFactory getErrorCodes
    INFO&#58; Looking up default SQLErrorCodes for DataSource
    20/10/2004 11&#58;23&#58;44 org.springframework.jdbc.support.SQLErrorCodesFactory getErrorCodes
    INFO&#58; Database product name found in cache for DataSource &#91;org.apache.commons.dbcp.BasicDataSource@1687e7c&#93;. Name is 'Firebird'.
    20/10/2004 11&#58;23&#58;44 org.springframework.beans.factory.support.AbstractBeanFactory getBean
    INFO&#58; Creating shared instance of singleton bean 'clienteManagerTarget'
    20/10/2004 11&#58;23&#58;44 org.springframework.beans.factory.support.AbstractBeanFactory getBean
    INFO&#58; Creating shared instance of singleton bean 'cidadeManagerTarget'
    20/10/2004 11&#58;23&#58;44 org.springframework.beans.factory.support.AbstractBeanFactory getBean
    INFO&#58; Creating shared instance of singleton bean 'clienteManager'
    20/10/2004 11&#58;23&#58;44 org.springframework.core.CollectionFactory <clinit>
    INFO&#58; Using JDK 1.4 collections
    20/10/2004 11&#58;23&#58;44 org.springframework.beans.factory.support.AbstractBeanFactory getBean
    INFO&#58; Creating shared instance of singleton bean 'cidadeManager'
    << Cadastro de Cidades >>
    Entre com um nome de cidade &#40;0 para sair&#41; - 0
    << Cadastro de Clientes >>
    Entre com o nome do cliente &#40;0 para sair&#41; - 0
    << Clientes >>
    20/10/2004 11&#58;23&#58;48 org.springframework.jdbc.datasource.JdbcTransactionObjectSupport <clinit>
    INFO&#58; JDBC 3.0 Savepoint class is available
    Hibernate&#58; select cliente0_.cliente_id as cliente_id, cliente0_.nome as nome, cliente0_.cidade_id as cidade_id from cliente cliente0_ order by  nome asc
    Hibernate&#58; select cidade0_.cidade_id as cidade_id0_, cidade0_.nome as nome0_ from cidade cidade0_ where cidade0_.cidade_id=?
    Hibernate&#58; select cidade0_.cidade_id as cidade_id0_, cidade0_.nome as nome0_ from cidade cidade0_ where cidade0_.cidade_id=?
    Hibernate&#58; select cidade0_.cidade_id as cidade_id0_, cidade0_.nome as nome0_ from cidade cidade0_ where cidade0_.cidade_id=?
    10	-98u -09hkjsblj	Santos
    1	Henrique	Santos
    16	Henrique Santos	Santos
    2	Jos	So Paulo
    3	adjkhlksdj	Santos
    4	kjl poiu	So Paulo
    15	ks-fhshsdlhfsd983iuj	So Paulo
    14	p9h-0983j2s	So Paulo
    11	p9uh0asd9uhsd09u	Santos
    13	paus0hd9uhs	Santos
    8	piuh-9hjlg	So Vicente
    5	pjn lkjashdl	So Paulo
    12	puhp99shd	Santos
    6	oijhpohp978	So Paulo
    7	piuh097	So Paulo
    9	lh-9h 9-	Santos
    
    
    << Cidades >>
    Hibernate&#58; select this.cidade_id as cidade_id0_, this.nome as nome0_ from cidade this where 1=1
    1	Santos
    20/10/2004 11&#58;23&#58;49 net.sf.hibernate.LazyInitializationException <init>
    SEVERE&#58; Failed to lazily initialize a collection - no session or session was closed
    net.sf.hibernate.LazyInitializationException&#58; Failed to lazily initialize a collection - no session or session was closed
    	at net.sf.hibernate.collection.PersistentCollection.initialize&#40;PersistentCollection.java&#58;209&#41;
    	at net.sf.hibernate.collection.PersistentCollection.read&#40;PersistentCollection.java&#58;71&#41;
    	at net.sf.hibernate.collection.Set.iterator&#40;Set.java&#58;130&#41;
    	at br.unisantos.engenharia.locadora.test.Main.main&#40;Main.java&#58;82&#41;
    net.sf.hibernate.LazyInitializationException&#58; Failed to lazily initialize a collection - no session or session was closed
    	at net.sf.hibernate.collection.PersistentCollection.initialize&#40;PersistentCollection.java&#58;209&#41;
    	at net.sf.hibernate.collection.PersistentCollection.read&#40;PersistentCollection.java&#58;71&#41;
    	at net.sf.hibernate.collection.Set.iterator&#40;Set.java&#58;130&#41;
    	at br.unisantos.engenharia.locadora.test.Main.main&#40;Main.java&#58;82&#41;
    Exception in thread "main"
    applicationContext.xml
    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http&#58;//www.springframework.org/dtd/spring-beans.dtd">
    
    <beans>
    
    	<!-- ========================= PERSISTENCE DEFINITIONS ========================= -->
    	
    	<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
    		<property name="driverClassName">
    			<value>org.firebirdsql.jdbc.FBDriver</value>
    			<!--<value>org.postgresql.Driver</value>-->
    			<!--<value>com.mysql.jdbc.Driver</value>-->
    		</property>
    		<property name="url">
    			<value>jdbc&#58;firebirdsql&#58;localhost/3050&#58;C&#58;/Henrique/firebird-1.5/databases/teste.gdb</value>
    			<!--<value>jdbc&#58;postgresql&#58;//localhost/teste</value>-->
    			<!--<value>jdbc&#58;mysql&#58;//localhost/teste</value>-->			
    		</property>
    		<property name="username">
    			<value>teste</value>
    		</property>
    		<property name="password">
    			<value>teste</value> 
    		</property>		
    	</bean>		
    
    	<bean id="sessionFactory" class="org.springframework.orm.hibernate.LocalSessionFactoryBean">
    		<property name="dataSource"><ref local="dataSource"/></property>
    		<property name="mappingResources">
    		  <list>
    		    <value>br/unisantos/engenharia/locadora/model/Cliente.hbm.xml</value>
    		    <value>br/unisantos/engenharia/locadora/model/Cidade.hbm.xml</value>
    		  </list>
    		</property>
    		<property name="hibernateProperties">
    		  <props>
    		  	<prop key="hibernate.dialect">net.sf.hibernate.dialect.FirebirdDialect</prop>
    		    <!--<prop key="hibernate.dialect">net.sf.hibernate.dialect.PostgreSQLDialect</prop>-->   
    		    <!--<prop key="hibernate.dialect">net.sf.hibernate.dialect.MySQLDialect</prop>-->
    		    <prop key="hibernate.show_sql">true</prop>
    		  </props>
    		</property>
    	</bean>
    
    	
    	<!-- ========================= TRANSACTIONS DEFINITIONS ========================= -->
    	<bean id="transactionManager" class="org.springframework.orm.hibernate.HibernateTransactionManager">
    		<property name="sessionFactory"><ref local="sessionFactory"/></property>
    	</bean>
    	
    	<!--
    	<bean id="TransactionManager" class="org.springframework.transaction.jta.JtaTransactionManager"/>
    	-->
    	
    
    	<!-- ========================= DATA ACCESS DEFINITIONS ========================= -->	
    
    	<bean id="clienteDAO" class="br.unisantos.engenharia.locadora.dao.ClienteDAOHibernate">
    		<property name="sessionFactory"><ref local="sessionFactory"/></property>
    	</bean>
    
    	<bean id="cidadeDAO" class="br.unisantos.engenharia.locadora.dao.CidadeDAOHibernate">
    		<property name="sessionFactory"><ref local="sessionFactory"/></property>
    	</bean>
    
    
    	<!-- ========================= SERVICES DEFINITIONS ========================= -->	
    
    	<bean id="clienteManagerTarget" class="br.unisantos.engenharia.locadora.service.ClienteManagerImpl">
    		<property name="clienteDAO"><ref local="clienteDAO"/></property>
    	</bean>
    		
    	<bean id="cidadeManagerTarget" class="br.unisantos.engenharia.locadora.service.CidadeManagerImpl">
    		<property name="cidadeDAO"><ref local="cidadeDAO"/></property>
    	</bean>
    		
    	<bean id="clienteManager" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
    		<property name="transactionManager"><ref local="transactionManager"/></property>
    		<property name="target"><ref local="clienteManagerTarget"/></property>		
    		<property name="transactionAttributes">
    			<props>
    				<prop key="save*">PROPAGATION_REQUIRED</prop>
    				<prop key="remove*">PROPAGATION_REQUIRED</prop>
    				<prop key="get*">PROPAGATION_REQUIRED, readOnly</prop>	
    			</props>
    		</property>
    	</bean>	
    	
    	<bean id="cidadeManager" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
    		<property name="transactionManager"><ref local="transactionManager"/></property>
    		<property name="target"><ref local="cidadeManagerTarget"/></property>
    		<property name="transactionAttributes">
    			<props>
    				<prop key="save*">PROPAGATION_REQUIRED</prop>
    				<prop key="remove*">PROPAGATION_REQUIRED</prop>
    				<prop key="get*">PROPAGATION_REQUIRED, readOnly</prop>	
    			</props>
    		</property>
    	</bean>
    </beans>
    Cidade.hbm.xml
    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 2.0//EN" "http&#58;//hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
    
    <hibernate-mapping>
    	<class name="br.unisantos.engenharia.locadora.model.Cidade" table="cidade">
    		<id name="cidadeId" column="cidade_id" type="int" unsaved-value="0">
    				
    			<!-- FIREBIRD -->
    			<generator class="native">
    				<param name="sequence">CIDADE_ID_GENERATOR</param>
    			</generator>
    			
    			<!-- POSTGRESQL -->
    			<!--
    			<generator class="native">
    				<param name="sequence">cidade_cidade_id_seq</param>
    			</generator>
    			-->
    			
    			<!-- MYSQL -->
    			<!-- <generator class="native"/> -->
    			
    		</id>
    		<property name="nome" column="nome" length="100" type="string" unique="true" not-null="true"/>
    		<set name="clientes" cascade="all" inverse="true" lazy="true">
          		<key column="cidade_id"/>
          		<one-to-many class="br.unisantos.engenharia.locadora.model.Cliente"/> 
        	</set>
    		
    	</class>
    </hibernate-mapping>

  • #2
    This means that you load your object in a first session without loading the associated objects.
    Then you try to access these objects in another session : this is not allowed;
    You have two choices :
    - load all the objects you need in the first session
    - use only one session (with the opensessioninviewfilter for example)

    Comment

    Working...
    X