英文:
Spring boot neo4j @NodeEntity with Integer property throwing error
问题
当我将字段rowStatusId的数据类型设置为Integer/int时,我可以使用.save()方法将节点保存到Neo4j数据库,但是在使用findByName(String name)检索记录时,系统会抛出以下错误(错误发生在service中的"LineItemStatus status = lineItemStatusRepository.findByName("New");"处)。当我将数据类型更改为Long时,我不会收到任何错误。
我们不能使用Integer吗?还是我需要包含其他任何依赖项?请帮助我。
错误信息:
Caused by: org.neo4j.ogm.exception.core.MappingException: Error mapping GraphModel
Caused by: org.springframework.core.convert.ConverterNotFoundException: 无法找到可从类型[java.lang.Long]转换为类型[int]的转换器
以下是我的代码:
import org.neo4j.ogm.annotation.GeneratedValue;
import org.neo4j.ogm.annotation.Id;
import org.neo4j.ogm.annotation.NodeEntity;
@NodeEntity
public class LineItemStatus {
@Id
@GeneratedValue
private Long id;
private String name;
private String description;
private int rowStatusId;
public LineItemStatus(String name, String description, int rowStatusId) {
this.name = name;
this.description = description;
this.rowStatusId = rowStatusId;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public int getRowStatusId() {
return rowStatusId;
}
public void setRowStatusId(int rowStatusId) {
this.rowStatusId = rowStatusId;
}
}
Repository:
import com.ns.tbe.model.nodes.LineItemStatus;
import org.springframework.data.neo4j.repository.Neo4jRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface LineItemStatusRepository extends Neo4jRepository<LineItemStatus, Long> {
LineItemStatus findByName(String name);
}
Service:
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class LineItemStatusService {
@Autowired
LineItemStatusRepository lineItemStatusRepository;
public void saveLineItemStatus() {
lineItemStatusRepository.save(new LineItemStatus("New", "new", 1));
lineItemStatusRepository.save(new LineItemStatus("Modified", "modified", 1));
lineItemStatusRepository.save(new LineItemStatus("Deleted", "deleted", 1));
LineItemStatus status = lineItemStatusRepository.findByName("New");
}
}
build.gradle文件:
plugins {
id 'org.springframework.boot' version '2.3.0.RELEASE'
id 'io.spring.dependency-management' version '1.0.9.RELEASE'
id 'java'
}
group = 'com.ns'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '11'
repositories {
mavenCentral()
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-data-neo4j'
compile group: 'org.neo4j', name: 'neo4j-ogm-bolt-driver', version: '3.2.11'
compile group: 'org.apache.commons', name: 'commons-lang3', version: '3.10'
implementation 'org.springframework.boot:spring-boot-starter'
testImplementation('org.springframework.boot:spring-boot-starter-test') {
exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
}
}
test {
useJUnitPlatform()
}
应用程序属性(application.properties):
spring.data.neo4j.uri=bolt://localhost:7687
spring.data.neo4j.username=neo4j
spring.data.neo4j.password=secret
英文:
When I use data type as Integer/int for field rowStatusId, I am able to save node to neo4j database using .save() method but while retrieving the record using findByName(String name), system throwing error as below (error occurred at "LineItemStatus status = lineItemStatusRepository.findByName("New");" in service ). I am not receiving any error when I change the data type to Long.
Can't we use Integer? or do I need to include any other dependencies? Please help me.
Caused by: org.neo4j.ogm.exception.core.MappingException: Error mapping GraphModel
Caused by: org.springframework.core.convert.ConverterNotFoundException: No converter found capable of converting from type [java.lang.Long] to type [int]
Below is my code
import org.neo4j.ogm.annotation.GeneratedValue;
import org.neo4j.ogm.annotation.Id;
import org.neo4j.ogm.annotation.NodeEntity;
@NodeEntity
public class LineItemStatus {
@Id
@GeneratedValue
private Long id;
private String name;
private String description;
private int rowStatusId;
public LineItemStatus(String name, String description, int rowStatusId) {
this.name = name;
this.description = description;
this.rowStatusId = rowStatusId;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public int getRowStatusId() {
return rowStatusId;
}
public void setRowStatusId(int rowStatusId) {
this.rowStatusId = rowStatusId;
}
}
reopsioty:
import com.ns.tbe.model.nodes.LineItemStatus;
import org.springframework.data.neo4j.repository.Neo4jRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface LineItemStatusRepository extends Neo4jRepository<LineItemStatus, Long> {
LineItemStatus findByName(String name);
}
Service:
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class LineItemStatusService {
@Autowired
LineItemStatusRepository lineItemStatusRepository;
public void saveLineItemStatus() {
lineItemStatusRepository.save(new LineItemStatus("New", "new", 1));
lineItemStatusRepository.save(new LineItemStatus("Modified", "modified", 1));
lineItemStatusRepository.save(new LineItemStatus("Deleted", "deleted", 1));
LineItemStatus status = lineItemStatusRepository.findByName("New");
}
}
build.gradle file:
plugins {
id 'org.springframework.boot' version '2.3.0.RELEASE'
id 'io.spring.dependency-management' version '1.0.9.RELEASE'
id 'java'
}
group = 'com.ns'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '11'
repositories {
mavenCentral()
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-data-neo4j'
compile group: 'org.neo4j', name: 'neo4j-ogm-bolt-driver', version: '3.2.11'
compile group: 'org.apache.commons', name: 'commons-lang3', version: '3.10'
implementation 'org.springframework.boot:spring-boot-starter'
testImplementation('org.springframework.boot:spring-boot-starter-test') {
exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
}
}
test {
useJUnitPlatform()
}
Application Properties:
spring.data.neo4j.uri=bolt://localhost:7687
spring.data.neo4j.username=neo4j
spring.data.neo4j.password=secret
答案1
得分: 1
驱动程序倾向于将整数存储为长整型。这里有一个详细的解释,说明了正在发生的情况:https://michael-simons.github.io/neo4j-sdn-ogm-tips/understand_the_type_system.html。
为了简化事情,您可能希望将 rowStatusId
更改为 long
类型。
英文:
Drivers tend to store integers as longs. Here is a nicely detailed explanation of what is going on: https://michael-simons.github.io/neo4j-sdn-ogm-tips/understand_the_type_system.html.
To simplify things, you may want to change rowStatusId
to be of type long
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论