编程语言
首页 > 编程语言> > java – 在运行时更改hibernate索引位置(lucene)时索引不会存储在新位置

java – 在运行时更改hibernate索引位置(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();
    }

和Bean正在关注

    
   
     

<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();

    }

解决方法:

尝试这样做:(对于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);

标签:java,hibernate,hibernate-search
来源: https://codeday.me/bug/20190629/1321176.html