问题
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.
回答1:
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
回答2:
I add Java Config for Neo4j with OGM Hibernate
@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;
}
}
And added to the pom (for Java 7 project)
<dependency>
<groupId>org.hibernate.ogm</groupId>
<artifactId>hibernate-ogm-neo4j</artifactId>
<version>4.1.0.Final</version>
</dependency>
来源:https://stackoverflow.com/questions/25341509/hibernate-ogm-provider-for-spring-configuration