I want to eagerly fetch a many-to-one association (Cat->Owner). The documentation (HiA, p.147) says "... So, if we want to reenable eager fetching for the association, now proxying is enabled, we would specify <many-to-one name="item" class="Item" outer-join="true"> ..."
Still if I execute the code, closing the session before reading the name property on the associated owner, an lazy initialization exception is thrown.
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" column="owner_id" outer-join="true"/>
</class>
<class name="test.Owner" proxy="test.Owner" table="OWNER">
<id name="id">
<generator class="increment"/>
</id>
<property name="name"/>
<set name="cats" lazy="false" inverse="true" cascade="save-update">
<key column="owner_id"/>
<one-to-many class="test.Cat"/>
</set>
</class>
</hibernate-mapping>
Code (between sessionFactory.openSession() and session.close()):
Session session = getSession();
List cats = session.find("from Cat");
List owners = session.find("from Owner");
session.close();
System.out.println("cats: ");
for (Iterator iter = cats.iterator(); iter.hasNext();) {
System.out.println(iter.next()); //includes owner.name
}
System.out.println("owners: ");
for (Iterator iter = owners.iterator(); iter.hasNext();) {
System.out.println(iter.next());
}
Full stack trace of any exception that occurs:
net.sf.hibernate.LazyInitializationException: Exception initializing proxy: [test.Owner#1]
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:27)
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
Debug level Hibernate log excerpt:
15:46:16,498=> INFO Environment - Hibernate 2.1.7
15:46:16,498=> INFO Environment - hibernate.properties not found
15:46:16,508=> INFO Environment - using CGLIB reflection optimizer
15:46:16,508=> INFO Environment - using JDK 1.4 java.sql.Timestamp handling
15:46:16,508=> INFO Configuration - Mapping file: mappings.xml
15:46:16,548=>DEBUG DTDEntityResolver - trying to locate
http://hibernate.sourceforge.net/hibern ... ng-2.0.dtd in classpath under net/sf/hibernate/
15:46:16,558=>DEBUG DTDEntityResolver - found
http://hibernate.sourceforge.net/hibern ... ng-2.0.dtd in classpath
15:46:16,668=> INFO Binder - Mapping class: test.Cat -> CAT
15:46:16,708=>DEBUG Binder - Mapped property: id -> id, type: long
15:46:16,718=>DEBUG Binder - Mapped property: name -> name, type: string
15:46:16,718=>DEBUG Binder - Mapped property: owner -> owner_id, type: test.Owner
15:46:16,718=> INFO Binder - Mapping class: test.Owner -> OWNER
15:46:16,718=>DEBUG Binder - Mapped property: id -> id, type: long
15:46:16,718=>DEBUG Binder - Mapped property: name -> name, type: string
15:46:16,728=>DEBUG Binder - Mapped property: cats, type: java.util.Set
15:46:16,728=> INFO Configuration - processing one-to-many association mappings
15:46:16,728=>DEBUG Binder - Second pass for collection: test.Owner.cats
15:46:16,728=> INFO Binder - Mapping collection: test.Owner.cats -> CAT
15:46:16,728=>DEBUG Binder - Mapped collection key: owner_id, one-to-many: test.Cat
15:46:16,728=> INFO Configuration - processing one-to-one association property references
15:46:16,728=> INFO Configuration - processing foreign key constraints
15:46:16,738=>DEBUG Configuration - resolving reference to class: test.Owner
15:46:16,748=> INFO Dialect - Using dialect: net.sf.hibernate.dialect.HSQLDialect
15:46:16,758=>DEBUG SQLExceptionConverterFactory - Using dialect defined converter
15:46:16,758=> INFO SettingsFactory - Use outer join fetching: true
15:46:16,758=> INFO DriverManagerConnectionProvider - Using Hibernate built-in connection pool (not for production use!)
15:46:16,758=> INFO DriverManagerConnectionProvider - Hibernate connection pool size: 20
15:46:16,768=> INFO DriverManagerConnectionProvider - using driver: org.hsqldb.jdbcDriver at URL: jdbc:hsqldb:hsql://localhost:9001/
15:46:16,768=> INFO DriverManagerConnectionProvider - connection properties: {user=sa, password=}
15:46:16,768=> INFO TransactionManagerLookupFactory - No TransactionManagerLookup configured (in JTA environment, use of process level read-write cache is not recommended)
15:46:16,768=>DEBUG DriverManagerConnectionProvider - total checked-out connections: 0
15:46:16,768=>DEBUG DriverManagerConnectionProvider - opening new JDBC connection
15:46:16,839=>DEBUG DriverManagerConnectionProvider - created connection to: jdbc:hsqldb:hsql://localhost:9001/, Isolation Level: 1
15:46:16,849=>DEBUG DriverManagerConnectionProvider - returning connection to pool, pool size: 1
15:46:16,849=> INFO SettingsFactory - Use scrollable result sets: true
15:46:16,849=> INFO SettingsFactory - Use JDBC3 getGeneratedKeys(): false
15:46:16,849=> INFO SettingsFactory - Optimize cache for minimal puts: false
15:46:16,849=> INFO SettingsFactory - Query language substitutions: {}
15:46:16,849=> INFO SettingsFactory - cache provider: net.sf.hibernate.cache.EhCacheProvider
15:46:16,849=> INFO Configuration - instantiating and configuring caches
15:46:16,939=> INFO SessionFactoryImpl - building session factory
15:46:16,939=>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}
15:46:17,029=> INFO ReflectHelper - reflection optimizer disabled for: test.Cat, BulkBeanException: Property is private (property setName)
15:46:17,039=> INFO ReflectHelper - reflection optimizer disabled for: test.Owner, BulkBeanException: Property is private (property setName)
15:46:17,249=>DEBUG SessionFactoryObjectFactory - initializing class SessionFactoryObjectFactory
15:46:17,249=>DEBUG SessionFactoryObjectFactory - registered: 402882c001af7ca60101af7ca7a10000 (unnamed)
15:46:17,249=> INFO SessionFactoryObjectFactory - Not binding factory to JNDI, no JNDI name configured
15:46:17,249=>DEBUG SessionFactoryImpl - instantiated session factory
15:46:17,289=>DEBUG SessionImpl - opened session
15:46:17,289=>DEBUG SessionImpl - find: from Cat
15:46:17,299=>DEBUG QueryTranslator - compiling query
15:46:17,309=>DEBUG SessionImpl - flushing session
15:46:17,309=>DEBUG SessionImpl - Flushing entities and processing referenced collections
15:46:17,309=>DEBUG SessionImpl - Processing unreferenced collections
15:46:17,309=>DEBUG SessionImpl - Scheduling collection removes/(re)creates/updates
15:46:17,309=>DEBUG SessionImpl - Flushed: 0 insertions, 0 updates, 0 deletions to 0 objects
15:46:17,309=>DEBUG SessionImpl - Flushed: 0 (re)creations, 0 updates, 0 removals to 0 collections
15:46:17,309=>DEBUG SessionImpl - Dont need to execute flush
15:46:17,309=>DEBUG QueryTranslator - HQL: from test.Cat
15:46:17,309=>DEBUG QueryTranslator - SQL: select cat0_.id as id, cat0_.name as name, cat0_.owner_id as owner_id from CAT cat0_
15:46:17,309=>DEBUG BatcherImpl - about to open: 0 open PreparedStatements, 0 open ResultSets
15:46:17,309=>DEBUG DriverManagerConnectionProvider - total checked-out connections: 0
15:46:17,309=>DEBUG DriverManagerConnectionProvider - using pooled JDBC connection, pool size: 0
15:46:17,309=>DEBUG SQL - select cat0_.id as id, cat0_.name as name, cat0_.owner_id as owner_id from CAT cat0_
15:46:17,309=>DEBUG BatcherImpl - preparing statement
15:46:17,329=>DEBUG Loader - processing result set
15:46:17,359=>DEBUG LongType - returning '1' as column: id
15:46:17,359=>DEBUG Loader - result row: 1
15:46:17,359=>DEBUG Loader - Initializing object from ResultSet: 1
15:46:17,359=>DEBUG Loader - Hydrating entity: test.Cat#1
15:46:17,359=>DEBUG StringType - returning 'klaartje' as column: name
15:46:17,359=>DEBUG LongType - returning '1' as column: owner_id
15:46:17,359=>DEBUG LongType - returning '2' as column: id
15:46:17,359=>DEBUG Loader - result row: 2
15:46:17,359=>DEBUG Loader - Initializing object from ResultSet: 2
15:46:17,359=>DEBUG Loader - Hydrating entity: test.Cat#2
15:46:17,359=>DEBUG StringType - returning 'pietje' as column: name
15:46:17,359=>DEBUG LongType - returning '1' as column: owner_id
15:46:17,359=>DEBUG LongType - returning '3' as column: id
15:46:17,359=>DEBUG Loader - result row: 3
15:46:17,359=>DEBUG Loader - Initializing object from ResultSet: 3
15:46:17,359=>DEBUG Loader - Hydrating entity: test.Cat#3
15:46:17,359=>DEBUG StringType - returning 'kees' as column: name
15:46:17,359=>DEBUG LongType - returning '2' as column: owner_id
15:46:17,359=>DEBUG Loader - done processing result set (3 rows)
15:46:17,359=>DEBUG BatcherImpl - done closing: 0 open PreparedStatements, 0 open ResultSets
15:46:17,359=>DEBUG BatcherImpl - closing statement
15:46:17,359=>DEBUG Loader - total objects hydrated: 3
15:46:17,359=>DEBUG SessionImpl - resolving associations for [test.Cat#1]
15:46:17,359=>DEBUG SessionImpl - loading [test.Owner#1]
15:46:17,369=>DEBUG SessionImpl - done materializing entity [test.Cat#1]
15:46:17,369=>DEBUG SessionImpl - resolving associations for [test.Cat#2]
15:46:17,369=>DEBUG SessionImpl - loading [test.Owner#1]
15:46:17,369=>DEBUG SessionImpl - done materializing entity [test.Cat#2]
15:46:17,369=>DEBUG SessionImpl - resolving associations for [test.Cat#3]
15:46:17,369=>DEBUG SessionImpl - loading [test.Owner#2]
15:46:17,369=>DEBUG SessionImpl - done materializing entity [test.Cat#3]
15:46:17,369=>DEBUG SessionImpl - initializing non-lazy collections
15:46:17,369=>DEBUG SessionImpl - find: from Owner
15:46:17,369=>DEBUG QueryTranslator - compiling query
15:46:17,369=>DEBUG SessionImpl - flushing session
15:46:17,369=>DEBUG SessionImpl - Flushing entities and processing referenced collections
15:46:17,369=>DEBUG SessionImpl - Processing unreferenced collections
15:46:17,369=>DEBUG SessionImpl - Scheduling collection removes/(re)creates/updates
15:46:17,369=>DEBUG SessionImpl - Flushed: 0 insertions, 0 updates, 0 deletions to 3 objects
15:46:17,369=>DEBUG SessionImpl - Flushed: 0 (re)creations, 0 updates, 0 removals to 0 collections
15:46:17,369=>DEBUG Printer - listing entities:
15:46:17,369=>DEBUG Printer - test.Cat{owner=Owner#2, name=kees, id=3}
15:46:17,369=>DEBUG Printer - test.Cat{owner=Owner#1, name=klaartje, id=1}
15:46:17,369=>DEBUG Printer - test.Cat{owner=Owner#1, name=pietje, id=2}
15:46:17,369=>DEBUG SessionImpl - Dont need to execute flush
15:46:17,369=>DEBUG QueryTranslator - HQL: from test.Owner
15:46:17,369=>DEBUG QueryTranslator - SQL: select owner0_.id as id, owner0_.name as name from OWNER owner0_
15:46:17,369=>DEBUG BatcherImpl - about to open: 0 open PreparedStatements, 0 open ResultSets
15:46:17,369=>DEBUG SQL - select owner0_.id as id, owner0_.name as name from OWNER owner0_
15:46:17,369=>DEBUG BatcherImpl - preparing statement
15:46:17,369=>DEBUG Loader - processing result set
15:46:17,369=>DEBUG LongType - returning '1' as column: id
15:46:17,369=>DEBUG Loader - result row: 1
15:46:17,369=>DEBUG Loader - Initializing object from ResultSet: 1
15:46:17,369=>DEBUG Loader - Hydrating entity: test.Owner#1
15:46:17,369=>DEBUG StringType - returning 'Karel' as column: name
15:46:17,369=>DEBUG LongType - returning '2' as column: id
15:46:17,369=>DEBUG Loader - result row: 2
15:46:17,369=>DEBUG Loader - Initializing object from ResultSet: 2
15:46:17,369=>DEBUG Loader - Hydrating entity: test.Owner#2
15:46:17,369=>DEBUG StringType - returning 'Maaike' as column: name
15:46:17,369=>DEBUG Loader - done processing result set (2 rows)
15:46:17,369=>DEBUG BatcherImpl - done closing: 0 open PreparedStatements, 0 open ResultSets
15:46:17,369=>DEBUG BatcherImpl - closing statement
15:46:17,369=>DEBUG Loader - total objects hydrated: 2
15:46:17,369=>DEBUG SessionImpl - resolving associations for [test.Owner#1]
15:46:17,369=>DEBUG SessionImpl - creating collection wrapper:[test.Owner.cats#1]
15:46:17,369=>DEBUG SessionImpl - done materializing entity [test.Owner#1]
15:46:17,369=>DEBUG SessionImpl - resolving associations for [test.Owner#2]
15:46:17,369=>DEBUG SessionImpl - creating collection wrapper:[test.Owner.cats#2]
15:46:17,369=>DEBUG SessionImpl - done materializing entity [test.Owner#2]
15:46:17,369=>DEBUG SessionImpl - initializing non-lazy collections
15:46:17,369=>DEBUG SessionImpl - initializing collection [test.Owner.cats#2]
15:46:17,369=>DEBUG SessionImpl - checking second-level cache
15:46:17,369=>DEBUG SessionImpl - collection not cached
15:46:17,369=>DEBUG BatcherImpl - about to open: 0 open PreparedStatements, 0 open ResultSets
15:46:17,369=>DEBUG SQL - select cats0_.owner_id as owner_id__, cats0_.id as id__, cats0_.id as id0_, cats0_.name as name0_, cats0_.owner_id as owner_id0_ from CAT cats0_ where cats0_.owner_id=?
15:46:17,369=>DEBUG BatcherImpl - preparing statement
15:46:17,379=>DEBUG LongType - binding '2' to parameter: 1
15:46:17,379=>DEBUG Loader - result set contains (possibly empty) collection: [test.Owner.cats#2]
15:46:17,379=>DEBUG SessionImpl - uninitialized collection: initializing
15:46:17,379=>DEBUG Loader - processing result set
15:46:17,379=>DEBUG LongType - returning '3' as column: id0_
15:46:17,379=>DEBUG Loader - result row: 3
15:46:17,379=>DEBUG LongType - returning '2' as column: owner_id__
15:46:17,379=>DEBUG Loader - found row of collection: [test.Owner.cats#2]
15:46:17,379=>DEBUG SessionImpl - reading row
15:46:17,379=>DEBUG LongType - returning '3' as column: id__
15:46:17,379=>DEBUG SessionImpl - loading [test.Cat#3]
15:46:17,379=>DEBUG SessionImpl - attempting to resolve [test.Cat#3]
15:46:17,379=>DEBUG SessionImpl - resolved object in session cache [test.Cat#3]
15:46:17,379=>DEBUG Loader - done processing result set (1 rows)
15:46:17,379=>DEBUG BatcherImpl - done closing: 0 open PreparedStatements, 0 open ResultSets
15:46:17,379=>DEBUG BatcherImpl - closing statement
15:46:17,379=>DEBUG Loader - total objects hydrated: 0
15:46:17,379=>DEBUG SessionImpl - 1 collections were found in result set
15:46:17,379=>DEBUG SessionImpl - collection fully initialized: [test.Owner.cats#2]
15:46:17,379=>DEBUG SessionImpl - 1 collections initialized
15:46:17,379=>DEBUG SessionImpl - collection initialized
15:46:17,379=>DEBUG SessionImpl - initializing collection [test.Owner.cats#1]
15:46:17,379=>DEBUG SessionImpl - checking second-level cache
15:46:17,379=>DEBUG SessionImpl - collection not cached
15:46:17,379=>DEBUG BatcherImpl - about to open: 0 open PreparedStatements, 0 open ResultSets
15:46:17,379=>DEBUG SQL - select cats0_.owner_id as owner_id__, cats0_.id as id__, cats0_.id as id0_, cats0_.name as name0_, cats0_.owner_id as owner_id0_ from CAT cats0_ where cats0_.owner_id=?
15:46:17,379=>DEBUG BatcherImpl - preparing statement
15:46:17,389=>DEBUG LongType - binding '1' to parameter: 1
15:46:17,389=>DEBUG Loader - result set contains (possibly empty) collection: [test.Owner.cats#1]
15:46:17,389=>DEBUG SessionImpl - uninitialized collection: initializing
15:46:17,389=>DEBUG Loader - processing result set
15:46:17,389=>DEBUG LongType - returning '1' as column: id0_
15:46:17,389=>DEBUG Loader - result row: 1
15:46:17,389=>DEBUG LongType - returning '1' as column: owner_id__
15:46:17,389=>DEBUG Loader - found row of collection: [test.Owner.cats#1]
15:46:17,389=>DEBUG SessionImpl - reading row
15:46:17,389=>DEBUG LongType - returning '1' as column: id__
15:46:17,389=>DEBUG SessionImpl - loading [test.Cat#1]
15:46:17,389=>DEBUG SessionImpl - attempting to resolve [test.Cat#1]
15:46:17,389=>DEBUG SessionImpl - resolved object in session cache [test.Cat#1]
15:46:17,389=>DEBUG LongType - returning '2' as column: id0_
15:46:17,389=>DEBUG Loader - result row: 2
15:46:17,389=>DEBUG LongType - returning '1' as column: owner_id__
15:46:17,389=>DEBUG Loader - found row of collection: [test.Owner.cats#1]
15:46:17,389=>DEBUG SessionImpl - reading row
15:46:17,389=>DEBUG LongType - returning '2' as column: id__
15:46:17,389=>DEBUG SessionImpl - loading [test.Cat#2]
15:46:17,389=>DEBUG SessionImpl - attempting to resolve [test.Cat#2]
15:46:17,389=>DEBUG SessionImpl - resolved object in session cache [test.Cat#2]
15:46:17,389=>DEBUG Loader - done processing result set (2 rows)
15:46:17,389=>DEBUG BatcherImpl - done closing: 0 open PreparedStatements, 0 open ResultSets
15:46:17,389=>DEBUG BatcherImpl - closing statement
15:46:17,389=>DEBUG Loader - total objects hydrated: 0
15:46:17,389=>DEBUG SessionImpl - 1 collections were found in result set
15:46:17,389=>DEBUG SessionImpl - collection fully initialized: [test.Owner.cats#1]
15:46:17,389=>DEBUG SessionImpl - 1 collections initialized
15:46:17,389=>DEBUG SessionImpl - collection initialized
15:46:17,389=>DEBUG SessionImpl - closing session
15:46:17,389=>DEBUG SessionImpl - disconnecting session
15:46:17,389=>DEBUG DriverManagerConnectionProvider - returning connection to pool, pool size: 1
15:46:17,389=>DEBUG SessionImpl - transaction completion