2014-07-07 54 views
4

我有需要在我的應用程序,我們需要存儲索引的基礎上的用戶,所以我試圖改變位置在運行時,但索引不獲取存儲在新的位置,如果我在配置文件中給出相同的位置,然後是我使用下面的代碼來改變位置當在運行時更改休眠索引位置(lucene)索引沒有獲取存儲在新位置

LocalSessionFactoryBean localSessionFactoryBean 

localSessionFactoryBean.getConfiguration() .setProperty("hibernate.search.default.indexBase", "New_loc") 
localSessionFactoryBean.getObject().getCurrentSession() //on this session object i am doing DAO opertation . 

休息的配置我在配置文件中已經給獲得存儲

。我投入了3天的時間尋找解決方案,但沒有取得成功。任何幫助將非常感激。 我的代碼來獲取會話以下

protected Session getSession() { 

     Configuration conf=sessionFactory.getConfiguration(); 


     conf.setProperty("hibernate.search.default.indexBase","c:\\testdata" /*CustomerContextHolder.getFile()!=null?CustomerContextHolder.getFile():defaultFileLocation*/); 

     ContextHolder.getOrBuildSearchFactory(conf); 
     return sessionFactory.getObject().getCurrentSession(); 
     // sessionFactory.getObject().openSession(); 
    } 

和豆製品是繼

<property name="hibernateProperties"> 
    <props> 
     <prop key="hibernate.dialect">${hibernate.dialect}</prop> 
     <prop key="hibernate.show_sql">${hibernate.show_sql}</prop> 
     <prop key="hibernate.jdbc.batch_size">${hibernate.jdbc.batch_size}</prop> 
     <prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop> <!-- Options are [validate, create, update, create-drop] --> 
     <!-- <prop key="hibernate.current_session_context_class">thread</prop> --> 

     <!-- Connection pool size --> 
     <prop key="hibernate.connection.pool_size">${hibernate.connection.pool_size}</prop> 

    issue --> 
     <prop key="hibernate.search.default.directory_provider">org.hibernate.search.store.impl.FSDirectoryProvider</prop> 
     <prop key="hibernate.search.default.locking_strategy">single</prop> 
    <prop key="hibernate.search.default.indexBase">c:\abc</prop> 
     <prop key="hibernate.search.lucene_version">LUCENE_35</prop> 
    </props> 

</property> 

更新代碼:

Session getSession() { 

     Configuration conf=sessionFactory.getConfiguration(); 

       conf.setProperty("hibernate.search.default.indexBase","c:\\testdata"); 
     //conf.configure(); Need to commented otherwise shwoing duplicate Property 

     ServiceRegistry serviceRegistry= new ServiceRegistryBuilder().applySettings(
       conf.getProperties()).buildServiceRegistry(); 
     return (Session) conf.buildSessionFactory(serviceRegistry).openSession(); 

    } 
+0

你有沒有試過調用'ContextHolder'的'getOrBuildSearchFactory'?例如'ContextHolder.getOrBuildSearchFactory(config)' – Amogh

+0

在使用'setProperty'改變hibernate屬性之後,你必須重新構建sessionFactory。 – Amogh

+0

感謝您的評論,所以我需要在localSessionFactoryBean.getObject()。getCurrentSession()和localSessionFactoryBean.getConfiguration().setProperty(「hibernate.search.default.indexBase」,「New_loc」)之前調用(getOrBuildSearchFactory)。它使交易問題becuasue我已經嘗試 斷開curent會話,然後嘗試使用openSession打開新seetio然後它給橫斷相關的錯誤 – user1047873

回答

2

嘗試這樣做:(Hibernate中4.x版)

Configuration cfg = localSessionFactoryBean.getConfiguration(); 
cfg .setProperty("hibernate.search.default.indexBase", "New_loc"); 
cfg.configure(); 
serviceRegistry = new ServiceRegistryBuilder().applySettings(
      cfg.getProperties()).build(); 
sessionFactory = cfg.buildSessionFactory(serviceRegistry); 
+1

還有一個問題,我發現有一段時間我以前的代碼工作,而有些時候它沒有將數據寫入索引 – user1047873

+0

@ user1047873,更新了根據Hibernate 4.x的代碼。這對我工作 – Amogh

+0

@Amough:真的非常感謝您的善意幫助。我會在晚上檢查這將更新你很快 – user1047873

1

我不知道指數基礎的這種動態變化是否會奏效。此外重建工廠似乎是不可靠的。你有沒有看看索引分片 - http://docs.jboss.org/hibernate/stable/search/reference/en-US/html_single/#advanced-features-dynamic-sharding。這應該允許您根據我們說的用戶ID分割數據。

如果分片沒有解決您的問題,那麼您可能可以優化您的實際用例。也許還有另一種解決方案。

+0

謝謝讓我檢查一下,因爲我不知道這個 – user1047873

+0

我無法更新4上的休眠版本。3.0.Final和陰影我需要使用此 – user1047873

+0

@ user1047873陰影?你什麼意思? – Sanne

0

經過了很多年後,我終於明白了原因。如果我在每個數據庫寫入後關閉sesson,那麼只有索引文件將被存儲在新的位置。然而,這不是有效的方法,但它是如何工作的