2016-06-19 41 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