package cn.search.pojo;
import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;
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
@Table(name = "search_foo")
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
@Indexed(index = "search_foo")
public class Foo implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
@Id
@DocumentId
@Field(name = "id", index = Index.TOKENIZED, store = Store.YES)
private Integer id;
@Column(nullable = false, length = 200)
@Field(name = "name", index = Index.TOKENIZED, store = Store.YES)
private String name;
@Column(nullable = false, length = 200)
@Field(name = "title", index = Index.TOKENIZED, store = Store.YES)
private String title;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
}
2、配置文件
org.hibernate.dialect.Oracle9Dialect
jdbc:oracle:thin:@192.168.0.21:1521:oradb
goodsres
goodsres
oracle.jdbc.driver.OracleDriver
org.hibernate.search.store.FSDirectoryProvider
e:/index
org.hibernate.cache.HashtableCacheProvider
3、测试代码
package cn.search.manager;
import static junit.framework.Assert.assertNotNull;
import static junit.framework.Assert.assertTrue;
import java.util.List;
import org.apache.lucene.analysis.StopAnalyzer;
import org.apache.lucene.queryParser.QueryParser;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.search.FullTextSession;
import org.hibernate.search.Search;
import org.junit.After;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import cn.search.pojo.Foo;
public class SearchResultsHibernate {
private static SessionFactory sf = null;
private static Session session = null;
private static Transaction tx = null;
@BeforeClass
public static void setupBeforeClass() throws Exception {
sf = new AnnotationConfiguration().configure("hibernate.cfg.xml")
.buildSessionFactory();
assertNotNull(sf);
}
@Before
public void setUp() throws Exception {
session = sf.openSession();
tx = session.beginTransaction();
tx.begin();
}
@After
public void tearDown() throws Exception {
tx.commit();
session.close();
}
public static void tearDownAfterClass() throws Exception {
if (sf != null)
sf.close();
}
@Test
public void testAddDept() throws Exception {
Foo foo = new Foo();
foo.setId(1);
foo.setName("第一个hibernate search");
foo.setTitle("好好学习,天天向上");
session.delete(foo);
}
@Test
public void testIndex() throws Exception {
FullTextSession fullTextSession = Search.createFullTextSession(session);
assertNotNull(session);
QueryParser parser = new QueryParser("title", new StopAnalyzer());
org.apache.lucene.search.Query luceneQuery = parser.parse("好好学习");
Query hibQuery = fullTextSession.createFullTextQuery(luceneQuery,
Foo.class);
List list = hibQuery.list();
assertTrue(list.size() > 0);
}
public static void main(String[] args) {
try {
setupBeforeClass();
SearchResultsHibernate searchResults = new SearchResultsHibernate();
searchResults.setUp();
searchResults.testAddDept();
searchResults.tearDown();
SearchResultsHibernate.tearDownAfterClass();
} catch (Exception e) {
e.printStackTrace();
}
}
}
posted on 2008-01-09 15:14 方涛升 阅读(1597) 评论(0) 编辑 收藏 所属分类: hibernate