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.  [ 6 posts ] 
Author Message
 Post subject: Criteria API: setFetchMode with createCriteria
PostPosted: Thu Aug 03, 2006 5:07 am 
Newbie

Joined: Thu Aug 03, 2006 4:50 am
Posts: 3
I have a object "Person" which is linked one-to-many with some "Contracts" objects and i use the Criteria API to initialize them.

If I use the API like it, the objets are well preloaded and it is ok:
Code:
Criteria criteriaPerson = hsession.createCriteria(Person.class);
criteriaPerson.setFetchMode("contracts",FetchMode.JOIN);

In this case, hibernate generate an left outer join in the query and i can access the contracts collection after closing the hibernate session.

But if i add a restriction in the query like this:

Code:
Criteria criteriaPerson = hsession.createCriteria(Person.class);
criteriaPerson.setFetchMode("contracts",FetchMode.JOIN);
criteriaPerson.createCriteria("contracts").add(Expression.isNotNull("endDate"));


In this case, hibernate generate an inner join in the query, and when i access the contracts collection after closing the hibernate session, i get a LazyInitializationException.

Any idea ?

PS: I don't want to define lazy loading properties in my xml files, cause i want to define lazy loading in the code with something like "setFetchMode("contracts",FetchMode.JOIN)".


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 03, 2006 9:24 am 
Newbie

Joined: Thu Aug 03, 2006 4:50 am
Posts: 3
:/.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 03, 2006 10:28 am 
Expert
Expert

Joined: Tue Apr 25, 2006 12:04 pm
Posts: 260
Have you tried the below code?

Code:
Criteria criteriaPerson = hsession.createCriteria(Person.class);
criteriaPerson.createCriteria( "contracts", Criteria.LEFT_JOIN ).add( Expression.isNotNull( "endDate" ) );


Top
 Profile  
 
 Post subject: The criteria is overrided
PostPosted: Fri Aug 04, 2006 2:16 am 
Newbie

Joined: Fri Aug 04, 2006 1:38 am
Posts: 4
Quote:
Criteria criteriaPerson = hsession.createCriteria(Person.class);
criteriaPerson.setFetchMode("contracts",FetchMode.JOIN);

// Now it is FetchMode.DEFAULT again
criteriaPerson.createCriteria("contracts").add(Expression.isNotNull("endDate"));


This will fetch all the Person objects who have contracts.endDate != null.
Since the Person to Contracts is a one-to-many collection, if you access contracts through the person object all the contracts of the person will have to be loaded. And since the session is closed LazyInitializationException will be thrown.

This should work:
Quote:
Criteria criteriaPerson = hsession.createCriteria(Person.class);
criteriaPerson.createCriteria( "contracts", Criteria.LEFT_JOIN ).add( Expression.isNotNull( "endDate" ) );


Top
 Profile  
 
 Post subject:
PostPosted: Fri Aug 04, 2006 4:20 am 
Newbie

Joined: Thu Aug 03, 2006 4:50 am
Posts: 3
Thanks for answers, but it doesn't seem to be nether a createCriteria(String,int) method in the hibernate 3 API, nor a Criteria.LEFT_JOIN attribute.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Aug 04, 2006 9:02 am 
Expert
Expert

Joined: Tue Apr 25, 2006 12:04 pm
Posts: 260
You are right, createCriteria( String, int ) was introduced in hibernate version 3.1 only and not available in hibernate versions 3.0.x


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