简体   繁体   English

使用Neo4J JDBC和MySQL的Spring Boot

[英]Spring Boot with Neo4J JDBC and MySQL

My Spring Boot Application is secured by Spring Security OAuth2. 我的Spring Boot应用程序由Spring Security OAuth2保护。 The userdata is stored in a SQL-database. 用户数据存储在SQL数据库中。 I followed here royclarkson's Oauth protected REST service. 我在这里遵循了royclarkson的Oauth保护的REST服务。 This project works with Spring Data JPA. 该项目与Spring Data JPA一起使用。 This works fine. 这很好。

https://github.com/royclarkson/spring-rest-service-oauth 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). 但是现在我想实现Neo4J配置,以通过Neo4J-JDBC(JDBC模板)从Neo4J数据库中获取数据。 Here I followed this GitHub project: 在这里,我关注了这个GitHub项目:

https://github.com/neo4j-examples/movies-java-spring-boot-jdbc 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: 我的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 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 :) 我知道,我是Spring的新手,但我希望有人能帮助我:)

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. 发生这种情况的原因是,由于Neo4j不是RDBMS数据库,并且默认情况下未提供方言,因此hibernate找不到Neo4J的任何方言。 You can use Hibernate OGM (search and include it in pom.xml), and then use following configuration to configure Entitymanager and Transaction manager 您可以使用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;
    }

}

But I suggest, to remove Hibernate altogether if you are not going to use RDBMS and will only be using Neo4j. 但是我建议,如果您不打算使用RDBMS并且仅使用Neo4j,请完全删除Hibernate。 Spring data has good support for NoSQL databases and Entities can be defined using annotations like @NodeEntity and @GraphId Spring数据对NoSQL数据库具有良好的支持,并且可以使用@NodeEntity和@GraphId之类的注释来定义实体

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM