-->
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.  [ 7 posts ] 
Author Message
 Post subject: Search Queries
PostPosted: Tue May 18, 2010 10:19 pm 
Newbie

Joined: Tue Feb 23, 2010 10:46 pm
Posts: 17
Hi,

I understand that Hibernate Search searches the indexed files and proceed to retrieve managed objects from the db based on the ID.
- May I know what is the reason/benefit for such move? to avoid mapping of association since lucene cannot support associations.
- How does HS retrieve from the db based on the ID? by batch or individual ID? (would like to know how the db connections, thus, performance affected)



I also understand the use of Projection will enforce the search to only retrieve from indexed files. However, the results would be an array of objects instead of the managed objects. I have found this example that could convert the array of objects back to managed objects:

org.hibernate.search.FullTextQuery query = s.createFullTextQuery( luceneQuery, Book.class );
query.setProjection( "title", "mainAuthor.name" );
query.setResultTransformer(
new StaticAliasToBeanResultTransformer( BookView.class, "title", "author" )
);
List<BookView> results = (List<BookView>) query.list();
for(BookView view : results) {
log.info( "Book: " + view.getTitle() + ", " + view.getAuthor() );
}

Pending to the above example,
1. Say, if Class A is a simple class containing no object attributes, Class A could be obtained as a managed object using Projection with the aid of ResultTransformer for all the attributes?
2. Say, Class A contain Class B, I use @IndexedEmbedded to annotate Class B. How should I set the projection so that I could obtained back Class A together with the embedded Class B?


Top
 Profile  
 
 Post subject: Re: Search Queries
PostPosted: Wed May 19, 2010 3:37 am 
Hibernate Team
Hibernate Team

Joined: Thu Apr 05, 2007 5:52 am
Posts: 1689
Location: Sweden
Quote:
I understand that Hibernate Search searches the indexed files and proceed to retrieve managed objects from the db based on the ID.
- May I know what is the reason/benefit for such move? to avoid mapping of association since lucene cannot support associations.

The reason is that as result of the search you want to have Hibernate managed objects. Meaning you can edit or delete the returned objects within your sesssion and the changes get persisted back into the db on transaction commit.

Quote:
- How does HS retrieve from the db based on the ID? by batch or individual ID? (would like to know how the db connections, thus, performance affected)

That depends on the query and whether you query a single index or multiple. Generally Search will try to load in batch, for example with a Criteria query using Restrictions.in.

Quote:
I also understand the use of Projection will enforce the search to only retrieve from indexed files. However, the results would be an array of objects instead of the managed objects. I have found this example that could convert the array of objects back to managed objects

Not quite. The example gives you back objects, but they are not managed, meaning they exist outside the Session scope.


--Hardy


Top
 Profile  
 
 Post subject: Re: Search Queries
PostPosted: Wed May 19, 2010 3:48 am 
Newbie

Joined: Tue Feb 23, 2010 10:46 pm
Posts: 17
Thanks for your prompt reply.

Regarding my first question, do you mean if the object is built from indexed files, it is not attached to the session, thus, could not be easily change and persist back to the db?

Could not see your reply for the final portion..


Top
 Profile  
 
 Post subject: Re: Search Queries
PostPosted: Wed May 19, 2010 4:01 am 
Hibernate Team
Hibernate Team

Joined: Thu Apr 05, 2007 5:52 am
Posts: 1689
Location: Sweden
Quote:
Regarding my first question, do you mean if the object is built from indexed files, it is not attached to the session, thus, could not be easily change and persist back to the db?

Exactly

Quote:
1. Say, if Class A is a simple class containing no object attributes, Class A could be obtained as a managed object using Projection with the aid of ResultTransformer for all the attributes?

No. You can create Class instances using projection and result transformer, but they are not managed.

Quote:
2. Say, Class A contain Class B, I use @IndexedEmbedded to annotate Class B. How should I set the projection so that I could obtained back Class A together with the embedded Class B?

You are free to project any fields you store in the index. Just make sure to get all fields you need to re-hydrate the objects. I recommend to build a little sample index and then open the index using the Lucene tool Luke. Looking at how the actual index looks like will help you to understand projection.

--Hardy


Top
 Profile  
 
 Post subject: Re: Search Queries
PostPosted: Wed May 19, 2010 4:11 am 
Newbie

Joined: Tue Feb 23, 2010 10:46 pm
Posts: 17
As i'm using the objects for displaying purposes, thus, I don't see a big issue that it is not managed.

However, I don't know how to "map" back the Class B attributes using ResultTransformer.
let's say I have projected: ClassA.w, ClassA.x, ClassB.y, ClassB.z

query.setResultTransformer(
new StaticAliasToBeanResultTransformer( ClassA.class, "w", "x", "ClassB.y", "ClassB.z" )
);

??


Top
 Profile  
 
 Post subject: Re: Search Queries
PostPosted: Wed May 19, 2010 4:19 am 
Hibernate Team
Hibernate Team

Joined: Thu Apr 05, 2007 5:52 am
Posts: 1689
Location: Sweden
For pure displaying purposes having unmanaged objects should be fine. If the name of the association for Class B in Class A is b you have to project b.y and b.z. I am not sure where you get the StaticAliasToBeanResultTransformer from and whether it can handle this. In the worst case you have to write your own result transformer.

--Hardy


Top
 Profile  
 
 Post subject: Re: Search Queries
PostPosted: Wed May 19, 2010 4:24 am 
Newbie

Joined: Tue Feb 23, 2010 10:46 pm
Posts: 17
Thanks so much! It has been a great help!


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