3

So far in all the documentations of Hibernate OGM I have never seen any example of setup using Spring's Java @Configuration. All the examples in the documentation and example projects use persistence.xml to configure the persistence-unit.

Is there any reason/limitation why Java config can't be used to achieve Hibernate OGM Persistence setup ?

Ideally I would like to convert the below persistence.xml into a java config:

    <!-- 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>
user4973
  • 85
  • 7

1 Answers1

4

I have achieved it, thanks to this tutorial http://allandequeiroz.io/2017/02/05/creating-jpa-entity-manager-programmatically-with-hibernate-and-cdi/

Now my setup looks like this

persistence.xml (still necessary with minimal configuration)

<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's Java Config

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

}

And then you just @Autowire EntityManager entityManager;

user4973
  • 85
  • 7