posts - 33,  comments - 11,  trackbacks - 0
          1、創建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("第一個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 方濤升 閱讀(1680) 評論(0)  編輯  收藏 所屬分類: hibernate

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


          網站導航:
           
          <2008年1月>
          303112345
          6789101112
          13141516171819
          20212223242526
          272829303112
          3456789

          常用鏈接

          留言簿(2)

          隨筆分類

          隨筆檔案

          文章分類

          搜索

          •  

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 佛山市| 菏泽市| 隆昌县| 冀州市| 江油市| 西丰县| 西和县| 图们市| 资中县| 拜城县| 新巴尔虎左旗| 广州市| 桓台县| 额济纳旗| 乌拉特中旗| 宁波市| 西青区| 甘泉县| 象山县| 裕民县| 石首市| 北辰区| 嘉鱼县| 合水县| 桑日县| 青浦区| 曲水县| 徐水县| 林周县| 元江| 封丘县| 巧家县| 迁西县| 柳河县| 永兴县| 长寿区| 甘洛县| 万州区| 电白县| 石首市| 宁波市|