2016-06-19 43 views
0

我的Spring Boot应用程序受Spring Security OAuth2的保护。用户数据存储在SQL数据库中。我跟着royclarkson的Oauth保护REST服务。该项目与Spring Data JPA一起工作。这工作正常。Spring使用Neo4J JDBC和MySQL启动

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

但现在我想实现我的Neo4j配置通过的Neo4j-JDBC(JDBC模板),以获得从我的Neo4j数据库的数据。在这里,我跟着这个项目的GitHub:

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

由于它的工作原理独立的应用程序,但如果我把这个两个项目togehter,我得到这个异常:

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' 

我Neo4jConfig.java样子这样的:

@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); 
} 

} 

我希望你们能理解我的问题。我知道,我是Spring的新手,但我希望任何人都可以帮助我:)

+0

当你说:“把这两个项目放在一起”时,你是什么意思?你能不能更好地阐述他们在什么时候工作,什么时候不工作? – visola

回答

0

发生这种情况是因为hibernate没有为Neo4J找到任何方言,因为Neo4j不是默认提供的RDBMS数据库和方言。您可以使用Hibernate OGM(搜索,并将其包含在pom.xml中),然后使用下列配置的EntityManager和事务管理器

@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; 
    } 

} 

但我建议,删除休眠完全,如果你不打算使用RDBMS只会使用Neo4j。 Spring数据对NoSQL数据库有很好的支持,实体可以使用@NodeEntity和@GraphId之类的注释来定义。

+0

非常感谢您的回答!正如你所说,我将开始使用Spring Data。 – Chris