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