Darshan Mehta
Darshan Mehta

Reputation: 30809

Unable to save Node to Neo4j with Spring boot

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:

Am I missing anything else here?

Upvotes: 3

Views: 337

Answers (1)

meistermeier
meistermeier

Reputation: 8262

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.

Upvotes: 1

Related Questions