【问题标题】:Is it possible to use Java Configuration rather than persistence.xml for Hibernate OGM Persistence setup?是否可以使用 Java Configuration 而不是 persistence.xml 进行 Hibernate OGM Persistence 设置?
【发布时间】:2017-10-24 20:14:12
【问题描述】:

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

Java config 不能用于实现 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>

【问题讨论】:

  • 你是说Spring的@Configuration注解吗?
  • 确实,我只是习惯称它为 java config。感谢您指出这一点
  • 应该和配置非OGM持久化单元没有太大区别,见stackoverflow.com/questions/1989672/…

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


【解决方案1】:

我做到了,感谢这个教程http://allandequeiroz.io/2017/02/05/creating-jpa-entity-manager-programmatically-with-hibernate-and-cdi/

现在我的设置如下所示

persistence.xml(用最少的配置仍然是必需的)

<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>

Spring 的 Java 配置

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

}

然后你只需@Autowire EntityManager entityManager;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-11
    • 2023-04-01
    • 2018-11-08
    • 1970-01-01
    • 2020-07-22
    • 1970-01-01
    相关资源
    最近更新 更多