-->
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.  [ 4 posts ] 
Author Message
 Post subject: How to Eagerly loading children in a FullTextQuery?
PostPosted: Sat Jun 09, 2007 12:09 pm 
Beginner
Beginner

Joined: Tue Nov 29, 2005 4:42 pm
Posts: 49
Location: Atlanta, GA
I'm using the Hibernate Search feature to create an index over two objects. It's a parent child relationship where the Parent hasMany children.

The relationship is set to lazy load, but I would like to eagerly load the children during a search query. Since the fullTextQuery is generated for me how can I specify to eagerly load the children in this case? BTW I know I can change the fetch type on the OneToMany annotation, but that would make it always eager. I would like to have more granular control over it.

Code:
FullTextSession fullTextSession = Search.createFullTextSession( session );
QueryParser parser = new QueryParser( "searchText", new StandardAnalyzer() );
org.hibernate.Query fullTextQuery = fullTextSession.createFullTextQuery( parser.parse( searchString ) );


Is there a way I can hand something to the createFullTextQuery() method in order to specify the eager relationships?

Thanks
Charlie


Top
 Profile  
 
 Post subject:
PostPosted: Sun Jun 10, 2007 9:47 pm 
Beginner
Beginner

Joined: Tue Nov 29, 2005 4:42 pm
Posts: 49
Location: Atlanta, GA
I think I've found a solution in the documentation. It's fresh off the press so it's no wonder I didn't see it until now. You have to have the latest version of search API 3.0.0 Beta 3.

The trick is to set the criteria of the query by doing the following:

Code:
FullTextSession fullTextSession = Search.createFullTextSession( session );
QueryParser parser = new MultiFieldQueryParser( searchFields, new StandardAnalyzer() );

// here is the magic.  First create a criteria and set the Fetch mode of the relationship.
Criteria criteria = session.createCriteria( Book.class ).setFetchMode( "authors", FetchMode.JOIN );
org.hibernate.Query fullTextQuery = fullTextSession.createFullTextQuery( parser.parse( searchString ) ).setCriteriaQuery( criteria );


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jun 11, 2007 7:19 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
That's correct, what do you think of this API BTW?

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jun 11, 2007 11:12 pm 
Beginner
Beginner

Joined: Tue Nov 29, 2005 4:42 pm
Posts: 49
Location: Atlanta, GA
Quote:
That's correct, what do you think of this API BTW?


Well I was very happy it was there so I really hadn't thought to hard about it, but since you asked I do have some feedback. The documentation says that really the only reason you should use setCriteriaQuery is to load associations, however, the API does not enforce this condition so the user could do something that's not allowed. Right now you have to read the docs to figure out that Criteria query isn't ment to be used for any old query. I would encapsulate the criteria creation behind the scenes so that it doesn't allow the user to get himself into trouble by doing things that the docs say don't do. Besides if it's there somebody will try to do something bizzare just becase they can. ;-)

For example:

Code:
List list = fullTextSession.createFullTextQuery( parser.parse( searchString ), MessageEntity.class ).eager( MessageEntity.class, "attachments" ).list();


Allow for multiple properties on a class.

Code:
List list = fullTextSession.createFullTextQuery( parser ).eager( Book.class, "authors", "pages" ).list();


Allow for eager() to be called more than once to load several relationships.

Code:
List list = fullTextSession.createFullTextQuery( parser ).include( RecordCollection.class, "linerNotes").eager( Album.class, "tracks" ).list();


You might want to allow for specifying to lazy load a relationship like:
Code:
List list = fullTextSession.createFullTextQuery( parser ).lazy( Book.class, "authors" ).list();


I use the terms eager and lazy to match up the the FetchMode annotations in JPA. Those are more about what the user wants to happen as opposed to the hibernate terms. Join and Select are what the library actually does to get a lazy or eager loading. They're just a little lower level lingo for my tastes.


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