问题
I have configured neo4j and cassandra repositories separately with spring boot using spring-data. However, when I try to use two repositories in the same projects it doesn't work as expected.
This is my folder structure.
-----org.test.project
-----controller
BarController
FooController
-----models
-----dao
-----cassandra
BarDAO
FooDAO
-----neo4j
BarDAO
FooDAO
-----repositories
-----cassandra
BarRepository
FooRepository
-----neo
BarRepository
FooRepository
-----services
CassandraService (Has cassandra repositories @Autowired)
NeoService(Has neo repositories @Autowired)
TestApp.java
Note that all the repositories extend respective spring-datarepository with respective DAO.
When I run with this configurations it gives the following error.
Field airportRepository in org.test.project.TestApp required a bean of type 'org.test.project.repositories.cassandra.BarRepository' that could not be found.
I tried changing the Repository names. Then it started to work.
First question is can't we have same names as they are in different packages and start working
Though it started to work this time it gave an error in the authentication header.
org.neo4j.ogm.drivers.http.request.HttpRequestException: http://localhost:7474/db/data/transaction/commit: No authentication header supplied.
I have already added ogm.properties, the same way I did when I was using neo4j repositories only. But it seems they no longer get applied. So I added following into application.properties.
spring.data.neo4j.password=neo4j
spring.data.neo4j.username=neo4j
Second question is, how can I configure neo4j just as the same way I did only with neo4j? I have defined following in ogm.properties. How can I apply this into neo4j configurations?
#Driver, required
driver=org.neo4j.ogm.drivers.bolt.driver.BoltDriver
#URI of the Neo4j database, required. If no port is specified, the
#default port 7687 is used. Otherwise, a port can be specified with
#bolt://neo4j:password@localhost:1234
URI=bolt://neo4j:neo4j@localhost
#Connection pool size (the maximum number of sessions per URL),
#optional, defaults to 50
connection.pool.size=150
#Encryption level (TLS), optional, defaults to REQUIRED. Valid
#values are NONE,REQUIRED
encryption.level=NONE
With the above changes, now it is giving following error.
org.neo4j.ogm.exception.MappingException: No identity field found for class: org.rozzie.processor.models.dao.cassandra.FlightDAO
Note that a neo4j.ogm exception is thrown for the cassandra models. What is happening under the hood. How can I configure these two databases with spring boot in one project as above?
回答1:
This looks like the Spring Boot autoconfiguration is not able to handle multiple Spring Data projects at the same time.
Please refer to documentation for Spring Data Neo4j and Spring Data Cassandra
In particular you should point SDN module to neo4j repositories only
@EnableNeo4jRepositories(basePackages = "org.test.project.repositories.neo")
and similarly for cassandra.
回答2:
I have used neo4j with mongo. I don't see any issues. I presume it should be same with cassandra. This is all the configuration I have
@SpringBootApplication
@EnableConfigurationProperties
@EnableNeo4jRepositories("com.in.neo4j.repository.neo")
@EnableMongoRepositories("com.in.neo4j.repository.mongo")
public class Neo4JApplication {
public static void main(String[] args) {
SpringApplication.run(Neo4JApplication.class, args);
}
}
And in my properties file I have
spring.data.neo4j.username=neo4j
spring.data.neo4j.password=admin
spring.data.mongodb.database=blah
spring.data.mongodb.host=blahblah
spring.data.mongodb.port=27017
来源:https://stackoverflow.com/questions/44033599/how-to-configure-neo4j-and-cassandra-repositories-in-same-spring-boot-applicatio