When eagerly fetching a proxied object, if the proxy has been cached, the proxy is returned and not the eagerly fetch object.
If the second lin is commented out, the Owner-Proxy is not stored in cache and the error is not produced.
Notice that the owner
is fetched when the query is executed, but is not correctly returned.
Hibernate version: 2.1.7
Mapping documents:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 2.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
<hibernate-mapping>
<class name="test.Cat" table="CAT">
<id name="id">
<generator class="increment"/>
</id>
<property name="name"/>
<many-to-one name="owner" class="test.Owner" column="owner_id" outer-join="true" cascade="all"/>
</class>
<class name="test.Owner" proxy="test.Owner" table="OWNER">
<id name="id">
<generator class="increment"/>
</id>
<property name="name"/>
</class>
</hibernate-mapping>
Code (between sessionFactory.openSession() and session.close()):
Session session = getSession();
session.load(Owner.class,new Long(2));
Cat cat = (Cat)session.load(Cat.class,new Long(3)); //references Owner#2
session.close();
System.out.println(cat); //includes owner.name
Full stack trace of any exception that occurs:
net.sf.hibernate.LazyInitializationException: Exception initializing proxy: [test.Owner#2]
at net.sf.hibernate.proxy.LazyInitializer.initializeWrapExceptions(LazyInitializer.java:64)
at net.sf.hibernate.proxy.LazyInitializer.getImplementation(LazyInitializer.java:164)
at net.sf.hibernate.proxy.CGLIBLazyInitializer.intercept(CGLIBLazyInitializer.java:108)
at test.Owner$$EnhancerByCGLIB$$ba744227.getName(<generated>)
at test.Cat.toString(Cat.java:28)
at java.lang.String.valueOf(Unknown Source)
at java.io.PrintStream.print(Unknown Source)
at java.io.PrintStream.println(Unknown Source)
at Shower.main(Shower.java:26)
Caused by: net.sf.hibernate.HibernateException: Could not initialize proxy - the owning Session was closed
at net.sf.hibernate.proxy.LazyInitializer.initialize(LazyInitializer.java:47)
at net.sf.hibernate.proxy.LazyInitializer.initializeWrapExceptions(LazyInitializer.java:60)
... 8 more
Name and version of the database you are using: hsqldb 1.7.2.2
The generated SQL (show_sql=true):
Hibernate: select cat0_.id as id1_, cat0_.name as name1_, cat0_.owner_id as owner_id1_, owner1_.id as id0_, owner1_.name as name0_ from CAT cat0_ left outer join OWNER owner1_ on cat0_.owner_id=owner1_.id where cat0_.id=?
Debug level Hibernate log excerpt:
13:33:00,624=> INFO Environment - Hibernate 2.1.7
13:33:00,624=> INFO Environment - hibernate.properties not found
13:33:00,624=> INFO Environment - using CGLIB reflection optimizer
13:33:00,624=> INFO Environment - using JDK 1.4 java.sql.Timestamp handling
13:33:00,634=> INFO Configuration - Mapping file: mappings.xml
13:33:00,674=>DEBUG DTDEntityResolver - trying to locate
http://hibernate.sourceforge.net/hibern ... ng-2.0.dtd in classpath under net/sf/hibernate/
13:33:00,674=>DEBUG DTDEntityResolver - found
http://hibernate.sourceforge.net/hibern ... ng-2.0.dtd in classpath
13:33:00,774=> INFO Binder - Mapping class: test.Cat -> CAT
13:33:00,824=>DEBUG Binder - Mapped property: id -> id, type: long
13:33:00,834=>DEBUG Binder - Mapped property: name -> name, type: string
13:33:00,834=>DEBUG Binder - Mapped property: owner -> owner_id, type: test.Owner
13:33:00,834=> INFO Binder - Mapping class: test.Owner -> OWNER
13:33:00,834=>DEBUG Binder - Mapped property: id -> id, type: long
13:33:00,834=>DEBUG Binder - Mapped property: name -> name, type: string
13:33:00,834=> INFO Configuration - processing one-to-many association mappings
13:33:00,834=> INFO Configuration - processing one-to-one association property references
13:33:00,834=> INFO Configuration - processing foreign key constraints
13:33:00,844=>DEBUG Configuration - resolving reference to class: test.Owner
13:33:00,854=> INFO Dialect - Using dialect: net.sf.hibernate.dialect.HSQLDialect
13:33:00,854=>DEBUG SQLExceptionConverterFactory - Using dialect defined converter
13:33:00,864=> INFO SettingsFactory - Use outer join fetching: true
13:33:00,874=> INFO DriverManagerConnectionProvider - Using Hibernate built-in connection pool (not for production use!)
13:33:00,874=> INFO DriverManagerConnectionProvider - Hibernate connection pool size: 20
13:33:00,874=> INFO DriverManagerConnectionProvider - using driver: org.hsqldb.jdbcDriver at URL: jdbc:hsqldb:hsql://localhost:9001/
13:33:00,874=> INFO DriverManagerConnectionProvider - connection properties: {user=sa, password=}
13:33:00,874=> INFO TransactionManagerLookupFactory - No TransactionManagerLookup configured (in JTA environment, use of process level read-write cache is not recommended)
13:33:00,874=>DEBUG DriverManagerConnectionProvider - total checked-out connections: 0
13:33:00,874=>DEBUG DriverManagerConnectionProvider - opening new JDBC connection
13:33:00,944=>DEBUG DriverManagerConnectionProvider - created connection to: jdbc:hsqldb:hsql://localhost:9001/, Isolation Level: 1
13:33:00,954=>DEBUG DriverManagerConnectionProvider - returning connection to pool, pool size: 1
13:33:00,954=> INFO SettingsFactory - Use scrollable result sets: true
13:33:00,954=> INFO SettingsFactory - Use JDBC3 getGeneratedKeys(): false
13:33:00,954=> INFO SettingsFactory - Optimize cache for minimal puts: false
13:33:00,954=> INFO SettingsFactory - Query language substitutions: {}
13:33:00,954=> INFO SettingsFactory - cache provider: net.sf.hibernate.cache.EhCacheProvider
13:33:00,954=> INFO Configuration - instantiating and configuring caches
13:33:01,034=> INFO SessionFactoryImpl - building session factory
13:33:01,034=>DEBUG SessionFactoryImpl - instantiating session factory with properties: {hibernate.connection.password=, hibernate.dialect=net.sf.hibernate.dialect.HSQLDialect, hibernate.connection.user=sa, hibernate.connection.url=jdbc:hsqldb:hsql://localhost:9001/, hibernate.connection.driver_class=org.hsqldb.jdbcDriver}
13:33:01,124=> INFO ReflectHelper - reflection optimizer disabled for: test.Owner, BulkBeanException: Property is private (property setName)
13:33:01,194=> INFO ReflectHelper - reflection optimizer disabled for: test.Cat, BulkBeanException: Property is private (property setName)
13:33:01,335=>DEBUG SessionFactoryObjectFactory - initializing class SessionFactoryObjectFactory
13:33:01,335=>DEBUG SessionFactoryObjectFactory - registered: 402882c001b429000101b42901970000 (unnamed)
13:33:01,335=> INFO SessionFactoryObjectFactory - Not binding factory to JNDI, no JNDI name configured
13:33:01,335=>DEBUG SessionFactoryImpl - instantiated session factory
13:33:01,365=>DEBUG SessionImpl - opened session
13:33:01,375=>DEBUG SessionImpl - loading [test.Owner#2]
13:33:01,375=>DEBUG SessionImpl - loading [test.Cat#3]
13:33:01,375=>DEBUG SessionImpl - attempting to resolve [test.Cat#3]
13:33:01,375=>DEBUG SessionImpl - object not resolved in any cache [test.Cat#3]
13:33:01,375=>DEBUG EntityPersister - Materializing entity: [test.Cat#3]
13:33:01,375=>DEBUG BatcherImpl - about to open: 0 open PreparedStatements, 0 open ResultSets
13:33:01,375=>DEBUG DriverManagerConnectionProvider - total checked-out connections: 0
13:33:01,375=>DEBUG DriverManagerConnectionProvider - using pooled JDBC connection, pool size: 0
13:33:01,375=>DEBUG SQL - select cat0_.id as id1_, cat0_.name as name1_, cat0_.owner_id as owner_id1_, owner1_.id as id0_, owner1_.name as name0_ from CAT cat0_ left outer join OWNER owner1_ on cat0_.owner_id=owner1_.id where cat0_.id=?
13:33:01,375=>DEBUG BatcherImpl - preparing statement
13:33:01,385=>DEBUG LongType - binding '3' to parameter: 1
13:33:01,425=>DEBUG Loader - processing result set
13:33:01,425=>DEBUG LongType - returning '2' as column: id0_
13:33:01,425=>DEBUG Loader - result row: 2, 3
13:33:01,425=>DEBUG Loader - Initializing object from ResultSet: 2
13:33:01,425=>DEBUG Loader - Hydrating entity: test.Owner#2
13:33:01,425=>DEBUG StringType - returning 'Maaike' as column: name0_
13:33:01,425=>DEBUG Loader - Initializing object from ResultSet: 3
13:33:01,425=>DEBUG Loader - Hydrating entity: test.Cat#3
13:33:01,425=>DEBUG StringType - returning 'kees' as column: name1_
13:33:01,425=>DEBUG LongType - returning '2' as column: owner_id1_
13:33:01,425=>DEBUG Loader - done processing result set (1 rows)
13:33:01,425=>DEBUG BatcherImpl - done closing: 0 open PreparedStatements, 0 open ResultSets
13:33:01,425=>DEBUG BatcherImpl - closing statement
13:33:01,425=>DEBUG Loader - total objects hydrated: 2
13:33:01,425=>DEBUG SessionImpl - resolving associations for [test.Owner#2]
13:33:01,425=>DEBUG SessionImpl - done materializing entity [test.Owner#2]
13:33:01,425=>DEBUG SessionImpl - resolving associations for [test.Cat#3]
13:33:01,425=>DEBUG SessionImpl - loading [test.Owner#2]
13:33:01,425=>DEBUG SessionImpl - attempting to resolve [test.Owner#2]
13:33:01,425=>DEBUG SessionImpl - resolved object in session cache [test.Owner#2]
13:33:01,425=>DEBUG SessionImpl - done materializing entity [test.Cat#3]
13:33:01,425=>DEBUG SessionImpl - initializing non-lazy collections
13:33:01,425=>DEBUG SessionImpl - closing session
13:33:01,425=>DEBUG SessionImpl - disconnecting session
13:33:01,425=>DEBUG DriverManagerConnectionProvider - returning connection to pool, pool size: 1
13:33:01,425=>DEBUG SessionImpl - transaction completion
13:33:01,435=>ERROR LazyInitializer - Exception initializing proxy