0
votes

My Spring Boot Application is secured by Spring Security OAuth2. The userdata is stored in a SQL-database. I followed here royclarkson's Oauth protected REST service. This project works with Spring Data JPA. This works fine.

https://github.com/royclarkson/spring-rest-service-oauth

But now I want to implement my Neo4J Configuration to get data from my Neo4J-Database via Neo4J-JDBC (JDBC-template). Here I followed this GitHub project:

https://github.com/neo4j-examples/movies-java-spring-boot-jdbc

As a standalone application it works, but if I put this two projects togehter, I get this Exception:

HibernateJpaAutoConfiguration.class]: Invocation of init method failed;
nested exception is org.hibernate.HibernateException: 
Unable to determine Dialect to use [name=Neo4j, majorVersion=3]; 
user must register resolver or explicitly set 'hibernate.dialect'

My Neo4jConfig.java looks like this:

@Configuration
public class Neo4jConfig {

//NEO4J Server Implementation via JDBC

private static final String NEO4J_URL = System.getProperty("NEO4J_URL","jdbc:neo4j://localhost:7474");
private static final String NEO4J_USER = System.getProperty("NEO4J_USER","neo4j");
private static final String NEO4J_PASSWORD = System.getProperty("NEO4J_PASSWORD","neo4j");

@Bean
public DataSource dataSource() {
    return new DriverManagerDataSource(NEO4J_URL, NEO4J_USER, NEO4J_PASSWORD);
}

public Neo4jConfig(){

}

public String getNeo4JURL(){
    return NEO4J_URL;
}
}

TripController.java

import hello.data.Trip;

@RestController
public class TripController {

@Autowired
JdbcTemplate template;

public static final RowMapper<Trip> TRIP_ROW_MAPPER = new RowMapper<Trip>() {
    public Trip mapRow(ResultSet rs, int rowNum) throws SQLException {
        return new Trip(rs.getString("tripname"),rs.getInt("slots"), rs.getInt("to_date"), rs.getInt("from_date"));
    }
};

String SEARCH_TRIPS_QUERY =
        " MATCH (t:Trip)\n" +
        " RETURN t.tripname as tripname, t.slots as slots, t.to_date as to_date, t.from_date as from_date";

@RequestMapping(path = "/alltrips", method = RequestMethod.GET)
public List<Trip> alltrips() {

    return template.query(SEARCH_TRIPS_QUERY, TRIP_ROW_MAPPER);
}

}

I hope you guys understand my question. I know, I am a really newone at Spring, but I hope anyone can help me :)

1
What do you mean when you say: "put the two projects together"? Can you elaborate better when they work and when they don't?visola

1 Answers

0
votes

This is happening because hibernate does not find any dialect for Neo4J as Neo4j is not RDBMS database and dialect is not provided by default. You can use Hibernate OGM (search and include it in pom.xml), and then use following configuration to configure Entitymanager and Transaction manager

@Configuration
@EnableJpaRepositories(basePackages = {
        "your repository packages" }, entityManagerFactoryRef = "n4jEntityManager", transactionManagerRef = "n4jTxnManager")
public class DatabaseConfiguration {


    @Bean(name = "n4jEntityManager")
    public LocalContainerEntityManagerFactoryBean entityManager() {

        Map<String, Object> properties = new HashMap<String, Object>();
        properties.put("javax.persistence.transactionType", "resource_local");
        properties.put("hibernate.ogm.datastore.provider","neo4j");
        properties.put("hibernate.ogm.datastore.host","localhost");
        properties.put("hibernate.ogm.datastore.port","7474");
        properties.put("hibernate.ogm.datastore.database", "your database");
        properties.put("hibernate.ogm.datastore.create_database", "true or false");

        LocalContainerEntityManagerFactoryBean entityManager = new LocalContainerEntityManagerFactoryBean();
        entityManager.setPackagesToScan("your domain packages");
        entityManager.setPersistenceUnitName("n4jPU");
        entityManager.setJpaPropertyMap(properties);
        entityManager.setPersistenceProviderClass(HibernateOgmPersistence.class);
        return entityManager;
    }

    @Bean(name = "n4jTxnManager")
    public PlatformTransactionManager txnManager() {
        JpaTransactionManager transactionManager = new JpaTransactionManager();
        transactionManager.setEntityManagerFactory(mongoEntityManager().getObject());
        return transactionManager;
    }

}

But I suggest, to remove Hibernate altogether if you are not going to use RDBMS and will only be using Neo4j. Spring data has good support for NoSQL databases and Entities can be defined using annotations like @NodeEntity and @GraphId