Spring 对于Hibernate OGM持久性设置,是否可以使用Java配置而不是persistence.xml?

Spring 对于Hibernate OGM持久性设置,是否可以使用Java配置而不是persistence.xml?,spring,jpa,spring-data-jpa,hibernate-ogm,Spring,Jpa,Spring Data Jpa,Hibernate Ogm,到目前为止,在Hibernate OGM的所有文档中,我从未见过任何使用Spring的Java@配置的安装示例。文档和示例项目中的所有示例都使用persistence.xml来配置持久化单元 Java配置不能用于实现Hibernate OGM持久性设置有任何原因/限制吗 理想情况下,我希望将下面的persistence.xml转换为java配置: <!-- Use the Hibernate OGM provider: configuration will be transpare

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

Java配置不能用于实现Hibernate OGM持久性设置有任何原因/限制吗

理想情况下,我希望将下面的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>


org.hibernate.ogm.jpa.hibernateogm持久性

多亏了本教程,我已经做到了这一点

现在,我的设置如下所示

persistence.xml(仍然需要最少的配置)

然后你只需要@Autowire
实体管理器实体管理器

你是说Spring的
@Configuration
注释吗?事实上,我习惯称它为java配置。感谢您指出,它与配置非OGM持久化单元应该没有太大区别,请参阅
<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>
@Bean
public Properties hibernateProperties(){
    final Properties properties = new Properties();

    properties.put("javax.persistence.provider", "org.hibernate.ogm.jpa.HibernateOgmPersistence");
    properties.put("javax.persistence.transactionType", "RESOURCE_LOCAL");

    properties.put("hibernate.ogm.datastore.provider", "redis_experimental");
    properties.put("hibernate.ogm.datastore.host", "127.0.0.1:6379");
    properties.put("hibernate.cache.use_second_level_cache", "true");
    properties.put("hibernate.cache.region.factory_class", "org.hibernate.cache.redis.hibernate5.SingletonRedisRegionFactory");
    properties.put("hibernate.cache.region_prefix", "hibernate");
    properties.put("hibernate.cache.use_query_cache", "true");
    properties.put("hibernate.cache.provider_configuration_file_resource_path", "hibernate-redis.properties");

    return properties;
}

@Bean
public EntityManager entityManager(Properties hibernateProperties){

    final EntityManagerFactory factory = Persistence.createEntityManagerFactory("ogm-jpa", hibernateProperties);
    return factory.createEntityManager();

}