Hibernate Books

All times are UTC - 5 hours [ DST ]



Post new topic Reply to topic  [ 4 posts ] 
Author Message
 Post subject: Access Neo4j with Spring Data using Hibernate JPA
PostPosted: Fri Mar 03, 2017 2:20 pm 
Newbie

Joined: Fri Mar 03, 2017 1:06 pm
Posts: 3
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;
}


Top
 Profile  
 
 Post subject: Re: Access Neo4j with Spring Data using Hibernate JPA
PostPosted: Mon Mar 06, 2017 6:31 am 
Hibernate Team
Hibernate Team

Joined: Fri Sep 09, 2011 3:18 am
Posts: 280
Here's the link to the official documentation for Hibernate OGM http://hibernate.org/ogm/documentation/.

The paragraph about configuring Neo4j is here: https://docs.jboss.org/hibernate/stable/ogm/reference/en-US/html_single/#_how_to_add_neo4j_integration

We don't have example with Spring Data though.


Top
 Profile  
 
 Post subject: Re: Access Neo4j with Spring Data using Hibernate JPA
PostPosted: Mon Mar 06, 2017 11:13 am 
Newbie

Joined: Thu Jan 05, 2017 1:47 pm
Posts: 18
I had to find things elsewhere on the web and modify as needed, but we have:

Code:
@Configuration("dbConfig")
@EnableJpaRepositories
@EnableTransactionManagement
public class DBConfig {
    @Bean(name = "entityManagerFactory")
    public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
        LocalContainerEntityManagerFactoryBean factoryBean = new LocalContainerEntityManagerFactoryBean();
        factoryBean.setPackagesToScan("com.myorg....", ...);
        factoryBean.setPersistenceUnitName("my-unit-name");

        Map<String, Object> jpaProps = new HashMap<>();
        jpaProps.putIfAbsent("javax.persistence.transactionType", "resource_local");
        jpaProps.putIfAbsent("hibernate.ogm.datastore.provider","neo4j_bolt");
        jpaProps.putIfAbsent("hibernate.ogm.datastore.host","localhost");
        jpaProps.putIfAbsent("hibernate.ogm.datastore.port","7687");
        jpaProps.putIfAbsent("hibernate.ogm.datastore.create_database", "true");
        factoryBean.setJpaPropertyMap(jpaProps);
        factoryBean.setPersistenceProviderClass(HibernateOgmPersistence.class);

        return factoryBean;
    }
}


Top
 Profile  
 
 Post subject: Re: Access Neo4j with Spring Data using Hibernate JPA
PostPosted: Tue Mar 07, 2017 5:34 am 
Hibernate Team
Hibernate Team

Joined: Fri Sep 09, 2011 3:18 am
Posts: 280
So, is it working now?


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 4 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.