用于 Spring 配置的 Hibernate OGM 提供程序 [英] Hibernate OGM provider for Spring configuration

查看:22
本文介绍了用于 Spring 配置的 Hibernate OGM 提供程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个可以使用SQL Server或Neo4j作为数据库的Java应用程序,不涉及应用层,我只是修改了provider和连接信息,如下:

I created a Java application that can use SQL Server or Neo4j as a database without touching the application layer, I just modify the provider and the connection information, like follows:

<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
             version="2.0">

    <persistence-unit name="jpa-tutorial" transaction-type="RESOURCE_LOCAL">
        <!--For SQL Server-->
        <!--provider>org.hibernate.ejb.HibernatePersistence</provider>
        <!--class>com.mycompany.hibernate.Atom</class-->

        <!--For Neo4j-->
        <provider>org.hibernate.ogm.jpa.HibernateOgmPersistence</provider>


        <properties>
            <!--For Neo4j-->
            <property name="hibernate.ogm.datastore.provider" value="neo4j_embedded" />
            <property name="hibernate.ogm.neo4j.database_path" value="D:/Stage/Neo4j/NEO4J_HOME_4/data/graph.db" />

            <!--For SQL Server-->
            <!--property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver" />
            <property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/atom" />
            <property name="javax.persistence.jdbc.user" value="root" />
            <property name="javax.persistence.jdbc.password" value="" />

            <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect" />
            <property name="hibernate.show_sql" value="true" />
            <property name="hibernate.hbm2ddl.auto" value="updatr" /-->
         </properties>

    </persistence-unit>
</persistence>

我现在必须使用 Spring 应用程序来做同样的事情.我已经开始学习 Spring,但发现了一个全新的逻辑.例如,有一个不同的 JPA 提供者:

I have now to make the same thing but with a Spring application. I've started learning Spring but found a completely new logic. For example, there is a different provider of JPA:

<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">

这是否意味着没有办法像第一个应用程序那样做?我的意思是没有 Hibernate OGM 提供程序可以代替 HibernateJpaVendorAdapter 来让应用程序在 Neo4j 而不是 SQL Server 上运行?

Does that mean that there's no way to do the same as the first application? I mean there's no Hibernate OGM provider that I can just put in the place of HibernateJpaVendorAdapter in order to make the application running on Neo4j rather than SQL Server?

提前致谢.

PS:我查看了 Spring Data,但发现定义实体(@NodeEntity、@GraphId、@RelatedTo 等)的另一个不同之处.我被要求不要接触应用程序代码.

PS: I checked out Spring Data but found another difference in defining entities (@NodeEntity, @GraphId, @RelatedTo, etc.). I'm asked not to touch the application code.

推荐答案

下面是Java配置类(注意我用的是spring boot,你可以根据自己的需要修改)

Here is the below Java configuration class(Note I'm using spring boot, you could modify according you your requirement)

@Configuration
@EnableJpaRepositories(basePackages = {
        "com.kp.swasthik.mongo.dao" }, entityManagerFactoryRef = "mongoEntityManager", transactionManagerRef = "mongoTransactionManager")
public class MongDbConfig {


    @Bean(name = "mongoEntityManager")
    public LocalContainerEntityManagerFactoryBean mongoEntityManager() throws Throwable {

        Map<String, Object> properties = new HashMap<String, Object>();
        properties.put("javax.persistence.transactionType", "resource_local");
        properties.put("hibernate.ogm.datastore.provider","mongodb");
        properties.put("hibernate.ogm.datastore.host","localhost");
        properties.put("hibernate.ogm.datastore.port","27017");
        properties.put("hibernate.ogm.datastore.database", "kpdb");
        properties.put("hibernate.ogm.datastore.create_database", "true");

        LocalContainerEntityManagerFactoryBean entityManager = new LocalContainerEntityManagerFactoryBean();
        entityManager.setPackagesToScan("com.kp.swasthik.mongo.domain");
        entityManager.setPersistenceUnitName("mongoPersistenceUnit");
        entityManager.setJpaPropertyMap(properties);
        entityManager.setPersistenceProviderClass(HibernateOgmPersistence.class);
        return entityManager;
    }

    @Bean(name = "mongoTransactionManager")
    public PlatformTransactionManager transactionManager() throws Throwable {
        JpaTransactionManager transactionManager = new JpaTransactionManager();
        transactionManager.setEntityManagerFactory(mongoEntityManager().getObject());
        return transactionManager;
    }

}

关于你关于@NodeEntity @GraphId 等的第二个问题.类似于 hibernate OGM sprig 为 no sql 提供 jpa 实现,使用 spring-data 为 nosql 数据存储的数量,如 redis、mongodb、cassandra、hbase、couchdb、solr、elasticsearch 等.@NodeEnity 和@GraphId 用于neo4j

Regarding your Second question on @NodeEntity @GraphId etc. Similar to hibernate OGM sprig provides jpa implementation for no sql using spring-data for number of nosql datastores such as redis, mongodb,cassandra, hbase, couchdb, solr, elasticsearch etc. @NodeEnity and @GraphId is used in neo4j

这篇关于用于 Spring 配置的 Hibernate OGM 提供程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆