-->
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.  [ 9 posts ] 
Author Message
 Post subject: Lazy initialization of object graph with query
PostPosted: Mon Dec 26, 2005 10:41 am 
Newbie

Joined: Mon Dec 26, 2005 7:58 am
Posts: 9
Hi,
My goal is to initialize every item in a lazy collection and for each item in the collection I would like to fetch also a lazy association (a many-to-one association).
E.g: Let A be the parent of my object graph. B is a collection in A (defined as lazy in its hbm file) and C is an object in B(i) (i for some item in B, also declared lazy).
I have tried to achieve that with different hql query within session.createQuery(…) or with createCriteria(...) with setFetchMode(...) etc.
Whatever fetching strategy I choose, I always result with some missing data in the web (you know…the famous LazyInitializationException of Hibernate) this is because I would like (and need!!) to fetch all the above data in my EJB container, closing the session and then passing all data to the web.
My question: Is there a way to fetch all data in class A + all items of collection B + for each item in B – fetching the many-to-one association C ?
Is it possible to do so with one query ?
BTW: I prefer not to declare my fetch strategy in the hbm file, but using it in runtime. Lazy is important for me since sometimes i need just a partial data (e.g need A without its collection B).

10x!!
Hibernate version: 3.0.5

simon.


Top
 Profile  
 
 Post subject: fetch all properties
PostPosted: Tue Dec 27, 2005 1:07 pm 
Expert
Expert

Joined: Fri Jul 22, 2005 2:42 pm
Posts: 670
Location: Seattle, WA
"fetch all properties" should help

http://www.hibernate.org/hib_docs/v3/re ... ryhql.html

_________________
--------------
Konstantin

SourceLabs - dependable OpenSource systems


Top
 Profile  
 
 Post subject: Re: fetch all properties
PostPosted: Thu Dec 29, 2005 2:30 am 
Newbie

Joined: Mon Dec 26, 2005 7:58 am
Posts: 9
kgignatyev wrote:


Thanks for replying but fetching all the data through "fetch all properties" works only if you are using bytecode instrumentation

From your link it sais:
Quote:
If you are using property-level lazy fetching (with bytecode instrumentation), it is possible to force Hibernate to fetch the lazy properties immediately (in the first query) using fetch all properties.


well...
1. I am not using bytecode instrumentation.
2. I don't know how to use it.
3. somehow it seems to me that there must be an alternative other than passing my code some instruments...(and I still want the lazy approch).

Any other idea?

Thanks!!


Top
 Profile  
 
 Post subject: bytecode instrumentation
PostPosted: Thu Dec 29, 2005 2:50 am 
Expert
Expert

Joined: Fri Jul 22, 2005 2:42 pm
Posts: 670
Location: Seattle, WA
Unless you have disabled bytecode instrumentation with something like
hibernate.cglib.use_reflection_optimizer=false
the bytecode instrumentation is enabled by default and H takes full advantage of it.
I think laziness works only when H is alloved to instrument classes

_________________
--------------
Konstantin

SourceLabs - dependable OpenSource systems


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 29, 2005 3:51 am 
Newbie

Joined: Mon Dec 26, 2005 7:58 am
Posts: 9
SORRY!!
but it's not working that way (I didn't disable reflection optimizer of cglib).
Try the simplest sample: Item and its (lazy) bids set/map. try to execute a query, something like:
Item anItem = session.createQuery("from item fetch all properties where id = 1").uniqueResult();
Assuming there is an item row with id 1, you will get only the item data and not the collection (even Hibernate.isInitialized(item.getBids()) will return false!)

???

Thanks!!!


Top
 Profile  
 
 Post subject: you are right
PostPosted: Thu Dec 29, 2005 1:30 pm 
Expert
Expert

Joined: Fri Jul 22, 2005 2:42 pm
Posts: 670
Location: Seattle, WA
You are right, it does not work. Looks like H bug.
In my test I also see that H reports that property is initialized, but attempt to read values fails.

The code:
public void testCollectionFetchAll() {
long id = createCollection();
Session s;
Transaction t;
CollectionHolder ch;
s = getSessions().openSession();
t = s.beginTransaction();
ch = (CollectionHolder) s.createQuery("from CollectionHolder fetch all properties").uniqueResult();
t.commit();
s.close();
System.out.println("fetch all bclasses Initialized::" +Hibernate.isPropertyInitialized( ch, "bclasses" ));
System.out.println("fetch all bclasses proxy Initialized::" +Hibernate.isInitialized( ch.getBclasses() ));
System.out.println("fetch all ch.bclasses.length = " + ch.getBclasses().size() );
System.out.println("fetch all ch.bclasses[1] = " + ch.getBclasses().get( 1 ) );
}

and output is:
fetch all bclasses Initialized::true
fetch all bclasses proxy Initialized::false
09:26:58,012 ERROR LazyInitializationException:19 - failed to lazily initialize a collection of role: org.hibernate.test.kgi.CollectionHolder.bclasses, no session or session was closed
org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: org.hibernate.test.kgi.CollectionHolder.bclasses, no session or session was closed
at org.hibernate.collection.AbstractPersistentCollection.throwLazyInitializationException(AbstractPersistentCollection.java:358)
at org.hibernate.collection.AbstractPersistentCollection.throwLazyInitializationExceptionIfNotConnected(AbstractPersistentCollection.java:350)
at org.hibernate.collection.AbstractPersistentCollection.readSize(AbstractPersistentCollection.java:97)
at org.hibernate.collection.PersistentList.size(PersistentList.java:91)
at org.hibernate.test.kgi.LazynessTest.testCollectionFetchAll(LazynessTest.java:51)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:585)
at junit.framework.TestCase.runTest(TestCase.java:154)
at org.hibernate.test.TestCase.runTest(TestCase.java:140)
at junit.framework.TestCase.runBare(TestCase.java:127)
at junit.framework.TestResult$1.protect(TestResult.java:106)
at junit.framework.TestResult.runProtected(TestResult.java:124)
at junit.framework.TestResult.run(TestResult.java:109)
at junit.framework.TestCase.run(TestCase.java:118)
at junit.framework.TestSuite.runTest(TestSuite.java:208)
at junit.framework.TestSuite.run(TestSuite.java:203)
at org.apache.tools.ant.taskdefs.optional.junit.JUnitTestRunner.run(JUnitTestRunner.java:297)
at org.apache.tools.ant.taskdefs.optional.junit.JUnitTestRunner.launch(JUnitTestRunner.java:672)
at org.apache.tools.ant.taskdefs.optional.junit.JUnitTestRunner.main(JUnitTestRunner.java:567)

_________________
--------------
Konstantin

SourceLabs - dependable OpenSource systems


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 29, 2005 7:07 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
"fetch all properties" is *not* meant to fetch associations or collections


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 29, 2005 7:13 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
Are you making use of the fetch keyword in HQL? It forces query-time loading of objects in collections. It's described in the "Associations and Joins" chapter of the ref docs, section 15.3 in my version.

You can also try Hibernate.initialize(), on the java side.


Top
 Profile  
 
 Post subject: hmm
PostPosted: Thu Dec 29, 2005 7:44 pm 
Expert
Expert

Joined: Fri Jul 22, 2005 2:42 pm
Posts: 670
Location: Seattle, WA
gavin wrote:
"fetch all properties" is *not* meant to fetch associations or collections


Gavin, then I suggest adding this note to #14.3 in the documentation, something like:
.... If you are using property-level lazy fetching (with bytecode instrumentation), it is possible to force Hibernate to fetch the lazy properties immediately (in the first query) using fetch all properties. Note: 'fetch all properties' has not affect associations, 'join fetch' should be used instead.

The paragraph #14.3 mentions that but it is easily overlooked during non-linear reading and 'fetch all properties' gives wrong idea regarding its meaning.

_________________
--------------
Konstantin

SourceLabs - dependable OpenSource systems


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 9 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.