Background
I have a Maven, Spring data Neo4j project. It posts and gets a single object. I am trying to add sessions, but running into errors.
There are docs: https://docs.spring.io/spring-data/neo4j/docs/current/reference/html/#reference:session
They suggest this:
@Configuration
@EnableNeo4jRepositories(basePackages = "org.neo4j.example.repository")
@EnableTransactionManagement
public class MyConfiguration {
@Bean
public SessionFactory sessionFactory() {
// with domain entity base package(s)
return new SessionFactory(configuration(), "org.neo4j.example.domain");
}
@Bean
public org.neo4j.ogm.config.Configuration configuration() {
ConfigurationSource properties = new ClasspathConfigurationSource("ogm.properties");
org.neo4j.ogm.config.Configuration configuration = new org.neo4j.ogm.config.Configuration.Builder(properties)
return configuration;
}
@Bean
public Neo4jTransactionManager transactionManager() {
return new Neo4jTransactionManager(sessionFactory());
}
}
But when I copy the second @Bean
my eclipse marks: new org.neo4j.ogm.config.Configuration.Builder(properties)
as red and suggests that I change type configuration to Builder. Then keeps suggesting changes until the 2nd @Bean
looks like:
@Bean
public Builder configuration() {
ConfigurationSource properties = new ClasspathConfigurationSource("ogm.properties");
Builder configuration = new org.neo4j.ogm.config.Configuration.Builder(properties);
return configuration;
}
But then the 1st @Bean
stops working because it expects a configuration()
.
Question
So, how do add a sessionfactory when the current doc's suggestion doesn't work? I have looked at many other examples, but they all use older versions with deprecated things like Neo4jConfiguration
Notes
My pom.xml has no parents and these dependencies and repositories:
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-neo4j</artifactId>
<version>5.0.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>2.0.0.M3</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot</artifactId>
<version>2.0.0.M3</version>
</dependency>
<dependency>
<groupId>com.voodoodyne.jackson.jsog</groupId>
<artifactId>jackson-jsog</artifactId>
<version>1.1</version>
<scope>compile</scope>
</dependency>
</dependencies>
<repositories>
<repository>
<id>spring-libs-release</id>
<name>Spring Releases</name>
<url>https://repo.spring.io/libs-release</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
UPDATE 1
Deleting all @Bean
s and trying this persons setup: Why does Neo4j OGM with Spring Boot 2.0.0.M4 apparently require the embedded driver?
like this:
@Configuration
@EnableNeo4jRepositories("com.myproject.repository")
@EnableTransactionManagement
public class ServiceRegistryConfiguration {
@Bean
public org.neo4j.ogm.config.Configuration configuration() {
return new org.neo4j.ogm.config.Configuration.Builder(new ClasspathConfigurationSource("ogm.properties")).build();
}
@Bean
public SessionFactory sessionFactory() {
return new SessionFactory(configuration(), "com.myproject.domain");
}
}
resulted in this error:
...Factory method 'configuration' threw exception; nested exception is java.lang.NullPointerException
my ogm.properties
file is:
server.port=8585
spring.data.neo4j.uri=bolt://localhost
spring.data.neo4j.username=myuser
spring.data.neo4j.password=mypass
Your initial configuration looks good, except there's a missing .build()
after the builder instantiation (it's a typo in the doc).
So the configuration init should be :
new org.neo4j.ogm.config.Configuration.Builder(properties).build();
About the 2nd : you cannot mix SDN style configuration and Spring Boot auto-configuration.
Either you use SDN style (as in your first code snippet) and the ogm.properties
file looks like :
URI=bolt://localhost
username=myUser
password=myPassword
Example here
OR you use Spring Boot auto-configuration then no need to declare the configuration, sessionFactory and transaction manager in the configuration class. Just annotate the configuration with
@EnableNeo4jRepositories("com.myproject.repository")
@EntityScan(basePackages = "com.myproject.domain")
and use the spring boot properties format :
spring.data.neo4j.uri=bolt://localhost
spring.data.neo4j.username=myuser
spring.data.neo4j.password=mypass
Example here
Regarding the NullPointerException
: it's because the ogm.properties
file is not found in the classpath.