gpt4 book ai didi

spring - 是否可以使用 Java 配置而不是 persistence.xml 进行 Hibernate OGM 持久性设置?

转载 作者:行者123 更新时间:2023-12-05 04:10:38 25 4
gpt4 key购买 nike

到目前为止,在 Hibernate OGM 的所有文档中,我从未见过任何使用 Spring 的 Java @Configuration 进行设置的示例。文档和示例项目中的所有示例都使用 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>

最佳答案

感谢本教程,我已经实现了 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 实体管理器实体管理器;

关于spring - 是否可以使用 Java 配置而不是 persistence.xml 进行 Hibernate OGM 持久性设置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44159146/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com