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

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

问题描述

我创建了一个Java应用程序,可以使用SQL Server或Neo4j作为数据库,而不需要触及应用程序层,我只需修改提供程序和连接信息,如下所示:

 <?xml version =1.0encoding =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-tutorialtransaction-type =RESOURCE_LOCAL>
<! - 对于SQL Server - >
<! - provider> org.hibernate.ejb.HibernatePersistence< / provider>
<! - class> com.mycompany.hibernate.Atom< / class - >

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


<属性>
<! - 对于Neo4j - >
< property name =hibernate.ogm.datastore.providervalue =neo4j_embedded/>
< property name =hibernate.ogm.neo4j.database_pathvalue =D:/Stage/Neo4j/NEO4J_HOME_4/data/graph.db/>

<! - 对于SQL Server - >
<! - property name =javax.persistence.jdbc.drivervalue =com.mysql.jdbc.Driver/>
< property name =javax.persistence.jdbc.urlvalue =jdbc:mysql:// localhost:3306 / atom/>
< property name =javax.persistence.jdbc.uservalue =root/>
< property name =javax.persistence.jdbc.passwordvalue =/>

< property name =hibernate.dialectvalue =org.hibernate.dialect.MySQLDialect/>
< property name =hibernate.show_sqlvalue =true/>
< / properties>

< / persistence-unit>
< /余辉>

我现在可以用Spring应用程序做同样的事情。我开始学习Spring,但发现了一个全新的逻辑。例如,JPA有另外一个提供者:

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

这是否意味着无法像第一个应用程序那样执行相同的操作?我的意思是没有Hibernate OGM提供程序,我可以放在 HibernateJpaVendorAdapter 的地方,以使应用程序在Neo4j上运行而不是在SQL Server上运行。



预先感谢。

PS:我查看了Spring Data,但发现定义实体的另一个区别(@NodeEntity,@GraphId,@ RelatedTo等)。我被要求不要触及应用程序代码。

解决方案

以下是Java配置类(注意我使用的是spring你可以根据你的要求进行修改)


$ @ $ $ $ b $ @ $ @ $ $ $ $ $ $ b $ 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);
返回entityManager;

$ b @Bean(name =mongoTransactionManager)
Public PlatformTransactionManager transactionManager()throws Throwable {
JpaTransactionManager transactionManager = new JpaTransactionManager();
transactionManager.setEntityManagerFactory(mongoEntityManager()。getObject());
返回transactionManager;
}

}

关于@NodeEntity的第二个问题@GraphId等。与hibernate类似,OGM sprig使用spring-data为redo,mongodb,cassandra,hbase,couchdb,solr,elasticsearch等nosql数据存储提供了jpa实现。@NodeEnity和@GraphId用于neo4j

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>

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

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?

Thanks in advance.

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.

解决方案

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

}

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天全站免登陆