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