I am trying to convert some tables to Neo4j.
With the code below I am able to access MySQL database with Spring Data using Hibernate JPA.
I want to be able to access Neo4j with Spring Data using Hibernate JPA. I am not running inside JavaEE container.
I cannot find an example or documentation on how to config Neo4j for Hibernate JPA. I tried using the example for Mongo DB but it doesn't work.
http://docs.jboss.org/hibernate/ogm/5.1 ... ml_single/Just running as a Java application
public static void main(String[] args) {
ApplicationContext context = null;
ApplicationContext ctx = null;
try {
App app = new App();
// System.out.println( "Hello World!" );
//ctx = new AnnotationConfigApplicationContext(Neo4jPersistenceContext.class, JpaPersistenceContext.class);
ctx = new AnnotationConfigApplicationContext(JpaPersistenceContext.class);
System.out.println("main() AnnotationConfigApplicationContext ctx: " + ctx);
//SessionFactory sessionFactory = ctx.getBean(SessionFactory.class);
//System.out.println("main() Neo4jPersistenceContext sessionFactory: " + sessionFactory);
LocalContainerEntityManagerFactoryBean lcemfb = ctx.getBean(LocalContainerEntityManagerFactoryBean.class);
Thanks
Stephen
@org.springframework.context.annotation.Configuration
@EnableTransactionManagement
@ComponentScan("com.dcubedev")
@PropertySource("classpath:application.properties")
@EnableJpaRepositories("com.dcubedev.repositories.jpa")
public class JpaPersistenceContext {
private static final String PROPERTY_NAME_DATABASE_DRIVER = "db.driver";
private static final String PROPERTY_NAME_DATABASE_PASSWORD = "db.password";
private static final String PROPERTY_NAME_DATABASE_URL = "db.url";
private static final String PROPERTY_NAME_DATABASE_USERNAME = "db.username";
private static final String PROPERTY_NAME_HIBERNATE_DIALECT = "hibernate.dialect";
private static final String PROPERTY_NAME_HIBERNATE_SHOW_SQL = "hibernate.show_sql";
private static final String PROPERTY_NAME_ENTITYMANAGER_PACKAGES_TO_SCAN = "entitymanager.packages.to.scan";
@Resource
private Environment env;
private Properties hibProperties() {
Properties properties = new Properties();
//properties.put(PROPERTY_NAME_HIBERNATE_DIALECT, "org.hibernate.dialect.MySQL57InnoDBDialect");
properties.put(PROPERTY_NAME_HIBERNATE_DIALECT, env.getRequiredProperty(PROPERTY_NAME_HIBERNATE_DIALECT));
properties.put(PROPERTY_NAME_HIBERNATE_SHOW_SQL, env.getRequiredProperty(PROPERTY_NAME_HIBERNATE_SHOW_SQL));
return properties;
}
@Bean
public DataSource dataSource() {
System.out.println("JpaPersistenceContext::dataSource() Environment env: " + env);
// capital60
// matalan
// mysql:mysql-connector-java:jar
DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName(env.getRequiredProperty(PROPERTY_NAME_DATABASE_DRIVER));
dataSource.setUrl(env.getRequiredProperty(PROPERTY_NAME_DATABASE_URL));
dataSource.setUsername(env.getRequiredProperty(PROPERTY_NAME_DATABASE_USERNAME));
dataSource.setPassword(env.getRequiredProperty(PROPERTY_NAME_DATABASE_PASSWORD));
return dataSource;
}
@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
//String persistenceXmlLocation = "C:/workspace/dcube-groups/dcube-groups/src/main/resources/persistence.xml";
String persistenceXmlLocation = "persistence.xml";
System.out.println("JpaPersistenceContext::entityManagerFactory() ... " );
System.out.println("JpaPersistenceContext::entityManagerFactory() Environment env: " + env);
HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
System.out.println("JpaPersistenceContext::entityManagerFactory() vendorAdapter: " + vendorAdapter);
vendorAdapter.setGenerateDdl(true);
LocalContainerEntityManagerFactoryBean factory = new LocalContainerEntityManagerFactoryBean();
System.out.println("JpaPersistenceContext::entityManagerFactory() LocalContainerEntityManagerFactoryBean factory: " + factory);
factory.setJpaVendorAdapter(vendorAdapter);
factory.setPackagesToScan("com.dcubedev.domain");
factory.setDataSource(dataSource());
factory.setPersistenceXmlLocation(persistenceXmlLocation);
factory.setJpaProperties(hibProperties());
System.out.println("JpaPersistenceContext::entityManagerFactory() factory.getObject(): " + factory.getObject());
return factory;
}
@Bean
public PlatformTransactionManager transactionManager() {
JpaTransactionManager txManager = new JpaTransactionManager();
txManager.setEntityManagerFactory(entityManagerFactory().getObject());
return txManager;
}