我创建了一个Java应用程序,可以使用SQL Server或Neo4j作为数据库,而无需触及应用程序层,我只需修改提供程序和连接信息,如下所示:
<?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提供者:
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
这是否意味着没有办法像第一个应用程序那样做?我的意思是我没有Hibernate OGM提供程序可以放在HibernateJpaVendorAdapter
的位置,以使应用程序在Neo4j而不是SQL Server上运行?
提前致谢。
PS:我查看了Spring Data但发现了定义实体的另一个区别(@NodeEntity,@ GraphId,@ RelatedTo等)。我要求不要触摸应用程序代码。
答案 0 :(得分:2)
以下是Java配置类(注意我使用spring boot,你可以根据你的要求进行修改)
@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使用spring-data为reds,mongodb,cassandra,hbase,couchdb,solr,elasticsearch等nosql数据存储提供jpa实现。 .NodeEnity和@GraphId用于neo4j
答案 1 :(得分:0)
我使用OGM Hibernate为Neo4j添加了Java Config
@Configuration
@EnableTransactionManagement
@JpaPackagesToScan(Entity.class)
public class RepositoryConfig {
/**
* Neo4J OGM EntityManager config
*/
@Bean
public LocalContainerEntityManagerFactoryBean entityManager(JpaPackagesToScanHolder holder) throws Throwable {
Map<String, Object> properties = new HashMap<String, Object>();
properties.put("javax.persistence.transactionType", "JTA");
properties.put("hibernate.ogm.datastore.provider", "neo4j_embedded");
properties.put("hibernate.ogm.datastore.database", "my-db");
properties.put("hibernate.ogm.neo4j.database_path", "/mnt/graph.db");
properties.put("hibernate.dialect", "org.hibernate.ogm.datastore.neo4j.Neo4jDialect");
LocalContainerEntityManagerFactoryBean entityManager = new LocalContainerEntityManagerFactoryBean();
entityManager.setPackagesToScan(holder.toStringArray());
entityManager.setPersistenceUnitName("my-pu");
entityManager.setJpaPropertyMap(properties);
entityManager.setPersistenceProviderClass(HibernateOgmPersistence.class);
return entityManager;
}
@Bean
public PlatformTransactionManager transactionManager(EntityManagerFactory entityManagerFactory) throws Throwable {
JpaTransactionManager transactionManager = new JpaTransactionManager();
transactionManager.setEntityManagerFactory(entityManagerFactory);
return transactionManager;
}
}
并添加到pom(用于Java 7项目)
<dependency>
<groupId>org.hibernate.ogm</groupId>
<artifactId>hibernate-ogm-neo4j</artifactId>
<version>4.1.0.Final</version>
</dependency>