-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: Problem with association initialization
PostPosted: Sat Feb 12, 2005 6:26 am 
Beginner
Beginner

Joined: Sun Sep 19, 2004 5:02 pm
Posts: 28
Location: Poland
Hibernate version:
2.1.8

Mapping documents:
I have 2 classes (A and B) and two associations between A and B (A<->B and A->B).
A(1)<->(*)B (bidirectional one-to-many)
A(*)->(1)B (singledirectional many-to-one)
All associations are lazy loaded.

Code between sessionFactory.openSession() and session.close():
In my transaction I get one object of class A (I use Session.get()).
I also initialize A(1)<->(*)B association (on the side of class A) (I use Hibernate.initialize(collection)).
And I commit transaction and object of class A is returned to higher layer.
Intencion was to get A class object with all B class object assigned to him by A(1)<->(*)B association.

Full stack trace of any exception that occurs:
In higer layer lazy initialization exception is thrown when I try to access properties of one of object of class B contained in collecion that represents A(1)<->(*)B association in class A.
I've noticed that this occurs only when this object is also in A(*)->(1)B association.
There is also a mesage that closed session is the reason of the problem.
In my case this object is a cglib proxy instance.

Name and version of the database you are using:
Oracle9i

The generated SQL (show_sql=true):
Two queries are generated.
First when I get A class object and second when I initialize collection for A(1)<->(*)B association.

_________________
Lmichasz


Top
 Profile  
 
 Post subject:
PostPosted: Sat Feb 12, 2005 5:56 pm 
Beginner
Beginner

Joined: Sun Sep 19, 2004 5:02 pm
Posts: 28
Location: Poland
I've done some tests and here are additional information about my problem
This time tests were done with Hibernate 2.1.6 and MySQL 4.0.13 (on 2.1.7 and 2.1.8 it looks identical)

Hibernate version:
2.1.6

Mapping documents:
<hibernate-mapping package="com.lmichasz.test.hibernate">

<class name="AImpl" proxy="A" table="A">

<id name="ID" column="ID" type="long">
<generator class="identity"/>
</id>

<property name="x" column="X" length="100" type="string" />

<many-to-one name="b" class="BImpl" column="B_ID" foreign-key="A_FK1"/>

<bag name="bs" inverse="true" lazy="true"
>
<key column="A_ID"/>
<one-to-many class="BImpl"/>
</bag>

</class>

</hibernate-mapping>

<hibernate-mapping package="com.lmichasz.test.hibernate">

<class name="BImpl" proxy="B" table="B">

<id name="ID" column="ID" type="long">
<generator class="identity"/>
</id>

<property name="x" column="X" length="100" type="string" />

<many-to-one name="a" class="AImpl" column="A_ID" foreign-key="B_FK1"/>

</class>

</hibernate-mapping>

Code between sessionFactory.openSession() and session.close():
Configuration c = new Configuration();
c.configure();
SessionFactory sf = c.buildSessionFactory();

Session s = sf.openSession();
Transaction t = s.beginTransaction();
A a = (A)s.get(AImpl.class,new Long(1));
Hibernate.initialize(a.getBs());
t.commit();
s.close();

B b = (B)a.getBs().get(0);
b.getX(); //<- Here exception is thrown

sf.close();

Full stack trace of any exception that occurs:
net.sf.hibernate.LazyInitializationException: Exception initializing proxy: [com.lmichasz.test.hibernate.BImpl#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 net.sf.hibernate.proxy.HibernateProxy$$EnhancerByCGLIB$$4daf392.getX(<generated>)
at com.lmichasz.test.hibernate.test.Test.main(Test.java:42)
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)
... 4 more

Name and version of the database you are using:
MySQL 4.0.13

The generated SQL (show_sql=true):
Hibernate: select aimpl0_.ID as ID0_, aimpl0_.X as X0_, aimpl0_.B_ID as B_ID0_ from A aimpl0_ where aimpl0_.ID=?
Hibernate: select bs0_.A_ID as A_ID__, bs0_.ID as ID__, bs0_.ID as ID0_, bs0_.X as X0_, bs0_.A_ID as A_ID0_ from B bs0_ where bs0_.A_ID=?

Debug level Hibernate log excerpt:
22:36:48,389 DEBUG SessionImpl:3260 - initializing collection [com.lmichasz.test.hibernate.AImpl.bs#1]
22:36:48,399 DEBUG SessionImpl:3261 - checking second-level cache
22:36:48,399 DEBUG SessionImpl:3267 - collection not cached
22:36:48,399 DEBUG BatcherImpl:200 - about to open: 0 open PreparedStatements, 0 open ResultSets
22:36:48,399 DEBUG SQL:226 - select bs0_.A_ID as A_ID__, bs0_.ID as ID__, bs0_.ID as ID0_, bs0_.X as X0_, bs0_.A_ID as A_ID0_ from B bs0_ where bs0_.A_ID=?
Hibernate: select bs0_.A_ID as A_ID__, bs0_.ID as ID__, bs0_.ID as ID0_, bs0_.X as X0_, bs0_.A_ID as A_ID0_ from B bs0_ where bs0_.A_ID=?
22:36:48,409 DEBUG BatcherImpl:249 - preparing statement
22:36:48,439 DEBUG Loader:402 - result set contains (possibly empty) collection: [com.lmichasz.test.hibernate.AImpl.bs#1]
22:36:48,449 DEBUG SessionImpl:3018 - uninitialized collection: initializing
22:36:48,449 DEBUG Loader:277 - processing result set
22:36:48,449 DEBUG Loader:480 - result row: 1
22:36:48,459 DEBUG Loader:611 - Initializing object from ResultSet: 1
22:36:48,459 DEBUG Loader:680 - Hydrating entity: com.lmichasz.test.hibernate.BImpl#1
22:36:48,459 DEBUG Loader:367 - found row of collection: [com.lmichasz.test.hibernate.AImpl.bs#1]
22:36:48,469 DEBUG SessionImpl:3041 - reading row
22:36:48,469 DEBUG SessionImpl:1986 - loading [com.lmichasz.test.hibernate.BImpl#1]
22:36:48,479 DEBUG SessionImpl:2083 - attempting to resolve [com.lmichasz.test.hibernate.BImpl#1]
22:36:48,479 DEBUG SessionImpl:2099 - resolved object in session cache [com.lmichasz.test.hibernate.BImpl#1]
22:36:48,479 DEBUG Loader:294 - done processing result set (1 rows)
22:36:48,479 DEBUG BatcherImpl:207 - done closing: 0 open PreparedStatements, 0 open ResultSets
22:36:48,499 DEBUG BatcherImpl:269 - closing statement
22:36:48,499 DEBUG Loader:314 - total objects hydrated: 1
22:36:48,509 DEBUG SessionImpl:2202 - resolving associations for [com.lmichasz.test.hibernate.BImpl#1]
22:36:48,509 DEBUG SessionImpl:1986 - loading [com.lmichasz.test.hibernate.AImpl#1]
22:36:48,509 DEBUG SessionImpl:2083 - attempting to resolve [com.lmichasz.test.hibernate.AImpl#1]
22:36:48,509 DEBUG SessionImpl:2099 - resolved object in session cache [com.lmichasz.test.hibernate.AImpl#1]
22:36:48,509 DEBUG SessionImpl:2226 - done materializing entity [com.lmichasz.test.hibernate.BImpl#1]
22:36:48,519 DEBUG SessionImpl:3077 - 1 collections were found in result set
22:36:48,519 DEBUG SessionImpl:3095 - collection fully initialized: [com.lmichasz.test.hibernate.AImpl.bs#1]
22:36:48,529 DEBUG SessionImpl:3098 - 1 collections initialized
22:36:48,529 DEBUG SessionImpl:3116 - initializing non-lazy collections
22:36:48,529 DEBUG SessionImpl:3269 - collection initialized
22:36:48,529 DEBUG JDBCTransaction:59 - commit
22:36:48,539 DEBUG SessionImpl:2246 - flushing session
22:36:48,539 DEBUG SessionImpl:2439 - Flushing entities and processing referenced collections
22:36:48,589 DEBUG SessionImpl:2884 - Collection found: [com.lmichasz.test.hibernate.AImpl.bs#1], was: [com.lmichasz.test.hibernate.AImpl.bs#1]
22:36:48,589 DEBUG SessionImpl:2780 - Processing unreferenced collections
22:36:48,589 DEBUG SessionImpl:2794 - Scheduling collection removes/(re)creates/updates
22:36:48,589 DEBUG SessionImpl:2270 - Flushed: 0 insertions, 0 updates, 0 deletions to 2 objects
22:36:48,599 DEBUG SessionImpl:2275 - Flushed: 0 (re)creations, 0 updates, 0 removals to 1 collections
22:36:48,599 DEBUG Printer:75 - listing entities:
22:36:48,599 DEBUG Printer:82 - com.lmichasz.test.hibernate.BImpl{a=AImpl#1, x=null, ID=1}
22:36:48,609 DEBUG Printer:82 - com.lmichasz.test.hibernate.AImpl{b=BImpl#1, bs=[BImpl#1], x=null, ID=1}
22:36:48,609 DEBUG SessionImpl:2359 - executing flush
22:36:48,619 DEBUG SessionImpl:2824 - post flush
22:36:48,619 DEBUG SessionImpl:585 - transaction completion
22:36:48,629 DEBUG SessionImpl:573 - closing session
22:36:48,629 DEBUG SessionImpl:3336 - disconnecting session
22:36:48,639 DEBUG DriverManagerConnectionProvider:120 - returning connection to pool, pool size: 1
22:36:48,639 DEBUG SessionImpl:585 - transaction completion
22:36:48,649 ERROR LazyInitializer:63 - Exception initializing proxy

_________________
Lmichasz


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.