-->
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.  [ 5 posts ] 
Author Message
 Post subject: why HibernateSearch fetch data from DB, if they were stored?
PostPosted: Fri Apr 11, 2008 3:59 am 
Newbie

Joined: Thu Apr 10, 2008 1:46 am
Posts: 7
Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp

Hibernate version:
hibernate-core-3.2.5.ga + hibernate-annotations-3.3.0.GA + hibernate-search-3.0.0.GA
Mapping documents:
package search.basic;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;

import org.hibernate.annotations.GenericGenerator;
import org.hibernate.search.annotations.DocumentId;
import org.hibernate.search.annotations.Field;
import org.hibernate.search.annotations.Index;
import org.hibernate.search.annotations.Indexed;
import org.hibernate.search.annotations.Store;


@Entity
@Indexed
@Table(name="STORETEXTCAT")
public class StoreTextCat implements java.io.Serializable {

/**
* serialVersionUID
*/
private static final long serialVersionUID = -8551166347723917709L;

@Id
@DocumentId
@GeneratedValue(generator = "hibseq")
@GenericGenerator(name = "hibseq", strategy = "increment")
@Column(name = "CID", unique = true, nullable = false, precision = 10, scale = 0)
private Integer cid;

@Field( index = Index.UN_TOKENIZED, store = Store.YES )
@Column(name = "NAME", nullable = false)
private String name;

@Field( index = Index.UN_TOKENIZED, store = Store.YES )
@Column(name = "PRICE", nullable = false, precision = 10, scale = 0)
private Integer price;

public StoreTextCat() {
}

public StoreTextCat(Integer price) {
this.price = price;
}

public StoreTextCat(Integer cid, String name, Integer price) {
this.cid = cid;
this.name = name;
this.price = price;
}

public Integer getCid() {
return this.cid;
}

public void setCid(Integer cid) {
this.cid = cid;
}

public String getName() {
return this.name;
}

public void setName(String name) {
this.name = name;
}

public Integer getPrice() {
return this.price;
}

public void setPrice(Integer price) {
this.price = price;
}
}
Code between sessionFactory.openSession() and session.close():
Session session = HibernateUtil.currentSession();
FullTextSession ftSession = Search.createFullTextSession( session );
Transaction tx = ftSession.beginTransaction();

/*
insert
*/
StoreTextCat cat = new StoreTextCat();
cat.setName( "StoredName" );
cat.setPrice( 100 );
ftSession.save( cat );

commitTx( tx );


/*
search
*/
tx = ftSession.beginTransaction();
QueryParser parser = new QueryParser( "name", new KeywordAnalyzer() );
org.apache.lucene.search.Query luceneQuery = null;
try{
//search the exactely the same name we just inserted
luceneQuery = parser.parse( "StoredName" );
}catch(Exception e){
e.printStackTrace();
}
FullTextQuery ftQuery = ftSession.createFullTextQuery( luceneQuery, StoreTextCat.class );
List results = ftQuery.list();

/* commit */
commitTx( tx );
FancyPrinter.getInstance().print( results.iterator(), true );

HibernateUtil.closeSession();
Full stack trace of any exception that occurs:
none
Name and version of the database you are using:
PostgreSQL Ver.8.3
The generated SQL (show_sql=true):
DB_TYPE=1
log4j:WARN No appenders could be found for logger (org.hibernate.cfg.annotations.Version).
log4j:WARN Please initialize the log4j system properly.
Hibernate: select max(CID) from STORETEXTCAT
Hibernate: insert into STORETEXTCAT (NAME, PRICE, CID) values (?, ?, ?)
commit...
Hibernate: select this_.CID as CID2_0_, this_.NAME as NAME2_0_, this_.PRICE as PRICE2_0_ from STORETEXTCAT this_ where (this_.CID in (?))
commit...
----------------------------
| Cid | Price | Name |
------+-------+-------------
| 1 | 100 | StoredName |
----------------------------
Debug level Hibernate log excerpt:


Problems with Session and transaction handling?

hi, I'm wondering why Hibernate Search still have to retrieve data from DB, if the original value of those data have already been stored inside Lucene index file. Shouldn't Hibernate Search just directly fetch the data from Lucene index file? instead of fetching the data from DB like this:
select this_.CID as CID2_0_, this_.NAME as NAME2_0_, this_.PRICE as PRICE2_0_ from STORETEXTCAT this_ where (this_.CID in (?))
or is there any reason why Hibernate Search does so?
Read this: http://hibernate.org/42.html


Last edited by atoman on Fri Apr 11, 2008 5:55 am, edited 1 time in total.

Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 11, 2008 5:32 am 
Hibernate Team
Hibernate Team

Joined: Thu Apr 05, 2007 5:52 am
Posts: 1689
Location: Sweden
Hi,

Hibernate Search has to hit the database per design. The idea is really to use Lucene to find potential matches, but then return fully managed objects. Maybe in your example you could rebuild the entity from the lucene index, but in many cases it won't be possible. Just think about collections and references to other entities.

That said, there is one feature which allows you to circumvent the db access. Have a look at projection queries - http://www.hibernate.org/hib_docs/search/reference/en/html_single/#projections. Note though you are not getting manages entities back.

--Hardy


Top
 Profile  
 
 Post subject: Thank you!
PostPosted: Fri Apr 11, 2008 5:54 am 
Newbie

Joined: Thu Apr 10, 2008 1:46 am
Posts: 7
Hi, hardy.ferentschik, thank you for answering my question, and it really helps me to understand why Hibernate Search doing so.

Thanks a lot!!


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 11, 2008 1:21 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
projection plus a resultTransformer is very close to what you want though.

_________________
Emmanuel


Top
 Profile  
 
 Post subject: what a nice word!
PostPosted: Fri Apr 11, 2008 9:29 pm 
Newbie

Joined: Thu Apr 10, 2008 1:46 am
Posts: 7
YES, "ResultTransformer", what a nice word to describe this returning encapsulated object! Plus, in most of the cases, the returning entity could not be rebuilt from the Lucene index just like what hardy.ferentschik mentioned.

emmanuel, Thanks a lot!!


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