posts - 33,  comments - 11,  trackbacks - 0
          1、創(chuàng)建POJO

          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、配置文件

          <?xml version="1.0" encoding="UTF-8"?>
          <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd" >
          <hibernate-configuration>
           <session-factory>
            <property name="hibernate.dialect">
             org.hibernate.dialect.Oracle9Dialect
            </property>
            <property name="hibernate.connection.url">
             jdbc:oracle:thin:@192.168.0.21:1521:oradb
            </property>
            <property name="hibernate.connection.username">
             goodsres
            </property>
            <property name="hibernate.connection.password">
             goodsres
            </property>
            <property name="hibernate.connection.driver_class">
             oracle.jdbc.driver.OracleDriver
            </property>

            <property name="hibernate.search.default.directory_provider">
             org.hibernate.search.store.FSDirectoryProvider
            </property>
            <property name="hibernate.search.default.indexBase">
             e:/index
            </property>
            <property name="hibernate.cache.provider_class">
             org.hibernate.cache.HashtableCacheProvider
            </property>

            <mapping class="cn.search.pojo.Foo" />

           </session-factory>

          </hibernate-configuration>
          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("第一個(gè)hibernate search");

            foo.setTitle("好好學(xué)習(xí),天天向上");

            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("好好學(xué)習(xí)");
            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 方濤升 閱讀(1679) 評論(0)  編輯  收藏 所屬分類: hibernate

          只有注冊用戶登錄后才能發(fā)表評論。


          網(wǎng)站導(dǎo)航:
           
          <2008年1月>
          303112345
          6789101112
          13141516171819
          20212223242526
          272829303112
          3456789

          常用鏈接

          留言簿(2)

          隨筆分類

          隨筆檔案

          文章分類

          搜索

          •  

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 青海省| 常州市| 铜川市| 东乌珠穆沁旗| 乐东| 陆丰市| 凤翔县| 民和| 临湘市| 蓝田县| 西宁市| 格尔木市| 耿马| 闻喜县| 安新县| 和硕县| 南开区| 泸定县| 深圳市| 宝鸡市| 睢宁县| 中宁县| 德安县| 方山县| 辽中县| 南阳市| 任丘市| 无棣县| 兰州市| 武平县| 阳西县| 庄浪县| 方正县| 石门县| 横山县| 绥阳县| 陕西省| 蓬溪县| 正宁县| 崇信县| 江城|