对于Hibernate OGM Persistence设置,是否可以使用Java Configuration而不是persistence.xml?

时间:2017-05-24 12:59:36

标签: spring jpa spring-data-jpa hibernate-ogm

到目前为止,在Hibernate OGM的所有文档中,我从未见过使用Spring的Java @Configuration进行任何设置的示例。文档和示例项目中的所有示例都使用persistence.xml来配置持久性单元。

为什么Java配置不能用于实现Hibernate OGM Persistence设置有什么原因/限制吗?

理想情况下,我想将下面的persistence.xml转换为java配置:

    <!-- Use the Hibernate OGM provider: configuration will be transparent -->
    <provider>org.hibernate.ogm.jpa.HibernateOgmPersistence</provider>
    <properties>
        <!-- Here you will pick which NoSQL technology to use, and configure it;
             in this example we start a local in-memory redis_experimental node. -->
        <property name="hibernate.ogm.datastore.provider" value="redis_experimental"/>
        <property name="hibernate.ogm.datastore.host" value="127.0.0.1:6379"/>
        <property name="hibernate.cache.use_second_level_cache" value="true"/>
        <property name="hibernate.cache.region.factory_class" value="org.hibernate.cache.redis.hibernate5.SingletonRedisRegionFactory"/>
        <property name="hibernate.cache.region_prefix" value="hibernate"/>
        <property name="hibernate.cache.use_query_cache" value="true"/>
        <property name="hibernate.cache.provider_configuration_file_resource_path" value="hibernate-redis.properties"/>

    </properties>
</persistence-unit>

1 个答案:

答案 0 :(得分:1)

由于本教程http://allandequeiroz.io/2017/02/05/creating-jpa-entity-manager-programmatically-with-hibernate-and-cdi/

,我已经实现了它

现在我的设置看起来像这样

persistence.xml(仍需要最小配置)

        string firstString = "one\two\three";
        char a = '\\';
        string[] splittedString = firstString.Split(a);
        foreach (string s in splittedString)
        {
            Console.WriteLine(s);
        }

Spring的Java Config

<persistence xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence
http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd"
             version="2.1">
    <persistence-unit name="ogm-jpa">
    </persistence-unit>
</persistence>

然后你就是@Autowire      EntityManager entityManager;