3
votes

I am trying to save a node into Neo4j database with Spring boot and getting the following Exception:

java.lang.IllegalArgumentException: Class class com.test.neo4j.model.Company is not a valid entity class. Please check the entity mapping.
at org.neo4j.ogm.session.delegates.SaveDelegate.save(SaveDelegate.java:77) ~[neo4j-ogm-core-3.2.11.jar:3.2.11]
    at org.neo4j.ogm.session.delegates.SaveDelegate.save(SaveDelegate.java:51) ~[neo4j-ogm-core-3.2.11.jar:3.2.11]
    at org.neo4j.ogm.session.Neo4jSession.save(Neo4jSession.java:480) ~[neo4j-ogm-core-3.2.11.jar:3.2.11]
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_65]

Below is my Entity class:

package com.test.neo4j.model;

import org.neo4j.ogm.annotation.Id;
import org.neo4j.ogm.annotation.NodeEntity;

@NodeEntity
public class Company {
    @Id
    private String id;
    private String name;
    public Company() {}

    public String getId() {
        return id;
    }
    public void setId(String id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
}

Below is my repository:

package com.test.neo4j.repository;

import org.springframework.data.neo4j.repository.Neo4jRepository;

import com.test.neo4j.model.Company;

public interface CompanyRepository extends Neo4jRepository<Company, String>{

}

I am Autowiring the repo in my service and calling save with it. Now, I have looked up the other answers and made sure of the following:

  • Package name does not contain any upper case character
  • Model class has got default constructor
  • id is explicitly set in the service

Am I missing anything else here?

1
My assumption is that the Company entity does not get scanned. Have you explicitly defined the scan path or put the entity class in a package with different (starting) name than your Spring Boot configuration / application class?meistermeier
That's absolutely right @meistermeier. I figured that out earlier but didn't post it. If you could post this as an answer, I will accept it.Darshan Mehta

1 Answers

1
votes

The Company class is not part of the class scanning initiated at start of the application. This can have various reasons:

  1. You are using the Spring Data Neo4j auto starter without any special config for package scans. As a result only entity classes in this package and "sub"-packages will get scanned.
  2. You manually configured the SessionFactory bean and the given package does not match the ("sub"-)package your class is in.