无法使用Spring Boot将Node保存到Neo4j

huangapple go评论147阅读模式
英文:

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&lt;Company, String&gt;{

}

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类不是在应用程序启动时发起的类扫描的一部分。
这可能有各种原因:

  1. 您正在使用Spring Data Neo4j自动启动器,而没有为包扫描进行任何特殊配置。因此,只会扫描此包及其“子”包中的实体类。
  2. 您手动配置了SessionFactorybean,给定的包与您的类所在的(“子”)包不匹配。
英文:

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.

huangapple
  • 本文由 发表于 2020年5月30日 09:00:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/62096599.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定