[英]Spring Boot with Neo4J JDBC and MySQL
我的Spring Boot應用程序由Spring Security OAuth2保護。 用戶數據存儲在SQL數據庫中。 我在這里遵循了royclarkson的Oauth保護的REST服務。 該項目與Spring Data JPA一起使用。 這很好。
https://github.com/royclarkson/spring-rest-service-oauth
但是現在我想實現Neo4J配置,以通過Neo4J-JDBC(JDBC模板)從Neo4J數據庫中獲取數據。 在這里,我關注了這個GitHub項目:
https://github.com/neo4j-examples/movies-java-spring-boot-jdbc
作為一個獨立的應用程序,它可以工作,但是如果將這兩個項目放在一起,則會出現以下異常:
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的新手,但我希望有人能幫助我:)
發生這種情況的原因是,由於Neo4j不是RDBMS數據庫,並且默認情況下未提供方言,因此hibernate找不到Neo4J的任何方言。 您可以使用Hibernate OGM(將其搜索並包含在pom.xml中),然后使用以下配置來配置Entitymanager和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;
}
}
但是我建議,如果您不打算使用RDBMS並且僅使用Neo4j,請完全刪除Hibernate。 Spring數據對NoSQL數據庫具有良好的支持,並且可以使用@NodeEntity和@GraphId之類的注釋來定義實體
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.