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
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 properties = new HashMap();
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