英文:
Unable to save Node to Neo4j with Spring boot
问题
我正在尝试使用Spring Boot将一个节点保存到Neo4j数据库中,但是遇到了以下异常:
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]
以下是我的实体类:
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;
}
}
以下是我的仓库类:
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>{
}
我在我的服务中使用@Autowire
自动装配了这个仓库,并调用了它的save
方法。现在,我已经查阅了其他答案,并确保了以下事项:
- 包名不包含任何大写字符
- 模型类具有默认构造函数
- 在服务中明确设置了
id
我还漏掉了其他什么吗?
英文:
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
得分: 1
Company
类不是在应用程序启动时发起的类扫描的一部分。
这可能有各种原因:
- 您正在使用Spring Data Neo4j自动启动器,而没有为包扫描进行任何特殊配置。因此,只会扫描此包及其“子”包中的实体类。
- 您手动配置了
SessionFactory
bean,给定的包与您的类所在的(“子”)包不匹配。
英文:
The Company
class is not part of the class scanning initiated at start of the application.
This can have various reasons:
- 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.
- You manually configured the
SessionFactory
bean and the given package does not match the ("sub"-)package your class is in.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论