Set dynamic index directory for Hibernate Search i

2019-07-27 09:07发布

This has been discussed already, however none of the solutions/advices worked for me. I want to configure the lucene search index path in Spring via persistence.xml. This is important, since the deployment server is (of course) different from my local machine, so paths will not match. Right now, my configuration of hibernate-search inside the persistence.xml looks like this:

<property name="hibernate.search.default.directory_provider" value="filesystem" /> 
<property name="tempdir" value="#{ systemProperties['java.io.tmpdir'] }" />
<property name="hibernate.search.default.indexBase" value="${tempdir}\hibernate\index" /> 

I've seen this...

http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/expressions.html#expressions-beandef-xml-based

...so it should work?! However, the variable is not replaced, and the files are written to a newly created subdir having the name ${tempdir}, which is not what I wanted :)

Thanks for your help!

1条回答
来,给爷笑一个
2楼-- · 2019-07-27 09:31

Before you look into this, please go through this explanation about how the persistence xml is read and used.

However, the field values in the persistence.xml are configurable through properties file, if you configure the LocalContainerEntityManagerFactoryBean in your spring context.

Using the jpaPropertyMap property of entity manager factory, it is possible to configure the values that are used in your persistence xml file.

Below is a sample configuration that is being used in my project.

<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <property name="jpaPropertyMap">
        <map>
            <entry key="hibernate.c3p0.min_size" value="5"/>
            <entry key="hibernate.c3p0.max_size" value="20"/>
            <entry key="hibernate.c3p0.timeout" value="1800"/>
            <entry key="hibernate.dialect" value="org.hibernate.dialect.MySQL5InnoDBDialect"/>
            <entry key="hibernate.search.default.indexBase" value="${index.directory}"/>
        </map>
    </property>
</bean>

In the above configuration hibernate.search.default.indexBase is being read from a properties file. And of course you need Spring's PropertyPlaceholderConfigurer to read the properties files.

Hope this helps.

查看更多
登录 后发表回答