Search code examples
javaneo4jspring-data-neo4j-4

neo4j null pointer exception while saving via @Repository


The sample project i'm working with can be found attached here - Spring Jira

This is my configuration

@EnableNeo4jRepositories(basePackages = "com.graph.repository")
public class DBConfig extends Neo4jConfiguration{
    @Value("${neo4j.location}")
    private String neo4jDatabaseLocation;

    @Override
    public SessionFactory getSessionFactory() {
        return new SessionFactory(getConfiguration(), "com.graph.entity");
    }

    @Bean
    public Configuration getConfiguration() {
        Configuration configuration = new Configuration();
        configuration.driverConfiguration()
            .setDriverClassName("org.neo4j.ogm.drivers.embedded.driver.EmbeddedDriver")
            .setURI(neo4jDatabaseLocation);
        return configuration;
    }

    @Bean
    @Override
    public Session getSession() throws Exception {
        return getSessionFactory().openSession();
    }
}

Abstract Entity

public abstract class Entity {
@GraphId
private Long id;

public Long getId() {
    return id;
}

@Override
public boolean equals(Object o) {
    if (this == o) return true;
    if (o == null || id == null || getClass() != o.getClass()) return false;

    Entity entity = (Entity) o;

    if (!id.equals(entity.id)) return false;

    return true;
}

@Override
public int hashCode() {
    return (id == null) ? -1 : id.hashCode();
}
}

This is my Entity

@NodeEntity(label = "Patient")
public class Patient extends Entity {
private String patientId;
private String patientName;
private String otherPatientId;
private String sex;
private String dateOfBirth;
private String patientIdIssuer;
@Relationship(type = "STUDY", direction = Relationship.UNDIRECTED)
private Set<Study> studies;

Getters and Setters...
}

Study has nested entity/relationship and that has another nested entity/relationship. 1:N relationship

This is my repository

@Repository
public interface PatientRepository extends GraphRepository<Patient> {
}

And this is the calling method

public class Test() {
    @Autowired
    private PatientRepository patientRepository;

    public void test() {
        Patient patient = new Patient();
        // set fields
        patientRepository.save(patient); -> This is where I get NPE
    }
}

Stack Trace :

Caused by: java.lang.NullPointerException: null
at org.neo4j.ogm.drivers.embedded.driver.EmbeddedDriver.nativeTransaction(EmbeddedDriver.java:180) ~[neo4j-ogm-embedded-driver-2.0.4.jar:na]
at org.neo4j.ogm.drivers.embedded.driver.EmbeddedDriver.newTransaction(EmbeddedDriver.java:148) ~[neo4j-ogm-embedded-driver-2.0.4.jar:na]
at org.neo4j.ogm.session.transaction.DefaultTransactionManager.openTransaction(DefaultTransactionManager.java:57) ~[neo4j-ogm-core-2.0.4.jar:na]
at org.neo4j.ogm.session.delegates.TransactionsDelegate.beginTransaction(TransactionsDelegate.java:37) ~[neo4j-ogm-core-2.0.4.jar:na]
at org.neo4j.ogm.session.Neo4jSession.beginTransaction(Neo4jSession.java:441) ~[neo4j-ogm-core-2.0.4.jar:na]
at org.neo4j.ogm.session.request.RequestExecutor.executeSave(RequestExecutor.java:84) ~[neo4j-ogm-core-2.0.4.jar:na]
at org.neo4j.ogm.session.delegates.SaveDelegate.save(SaveDelegate.java:75) ~[neo4j-ogm-core-2.0.4.jar:na]
at org.neo4j.ogm.session.delegates.SaveDelegate.save(SaveDelegate.java:44) ~[neo4j-ogm-core-2.0.4.jar:na]
at org.neo4j.ogm.session.Neo4jSession.save(Neo4jSession.java:425) ~[neo4j-ogm-core-2.0.4.jar:na]

Can someone please tell me what I'm doing wrong??

Note : I had this working earlier with sdn.3.x with GraphDatabaseService


Solution

  • Looks like the only thing missing is the @Configuration annotation on your Neo4jConfiguration class:

    @org.springframework.context.annotation.Configuration
    @EnableTransactionManagement
    @EnableNeo4jRepositories(basePackages = "com.seyfert.matrix.graph.repository")
    public class DBConfig extends Neo4jConfiguration{
    ...