春季启动 Neo4j @NodeEntity 中的 Integer 属性引发错误。

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

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]的转换器

以下是我的代码:

  1. import org.neo4j.ogm.annotation.GeneratedValue;
  2. import org.neo4j.ogm.annotation.Id;
  3. import org.neo4j.ogm.annotation.NodeEntity;
  4. @NodeEntity
  5. public class LineItemStatus {
  6. @Id
  7. @GeneratedValue
  8. private Long id;
  9. private String name;
  10. private String description;
  11. private int rowStatusId;
  12. public LineItemStatus(String name, String description, int rowStatusId) {
  13. this.name = name;
  14. this.description = description;
  15. this.rowStatusId = rowStatusId;
  16. }
  17. public Long getId() {
  18. return id;
  19. }
  20. public void setId(Long id) {
  21. this.id = id;
  22. }
  23. public String getName() {
  24. return name;
  25. }
  26. public void setName(String name) {
  27. this.name = name;
  28. }
  29. public String getDescription() {
  30. return description;
  31. }
  32. public void setDescription(String description) {
  33. this.description = description;
  34. }
  35. public int getRowStatusId() {
  36. return rowStatusId;
  37. }
  38. public void setRowStatusId(int rowStatusId) {
  39. this.rowStatusId = rowStatusId;
  40. }
  41. }

Repository:

  1. import com.ns.tbe.model.nodes.LineItemStatus;
  2. import org.springframework.data.neo4j.repository.Neo4jRepository;
  3. import org.springframework.stereotype.Repository;
  4. @Repository
  5. public interface LineItemStatusRepository extends Neo4jRepository<LineItemStatus, Long> {
  6. LineItemStatus findByName(String name);
  7. }

Service:

  1. import org.springframework.beans.factory.annotation.Autowired;
  2. import org.springframework.stereotype.Service;
  3. @Service
  4. public class LineItemStatusService {
  5. @Autowired
  6. LineItemStatusRepository lineItemStatusRepository;
  7. public void saveLineItemStatus() {
  8. lineItemStatusRepository.save(new LineItemStatus("New", "new", 1));
  9. lineItemStatusRepository.save(new LineItemStatus("Modified", "modified", 1));
  10. lineItemStatusRepository.save(new LineItemStatus("Deleted", "deleted", 1));
  11. LineItemStatus status = lineItemStatusRepository.findByName("New");
  12. }
  13. }

build.gradle文件:

  1. plugins {
  2. id 'org.springframework.boot' version '2.3.0.RELEASE'
  3. id 'io.spring.dependency-management' version '1.0.9.RELEASE'
  4. id 'java'
  5. }
  6. group = 'com.ns'
  7. version = '0.0.1-SNAPSHOT'
  8. sourceCompatibility = '11'
  9. repositories {
  10. mavenCentral()
  11. }
  12. dependencies {
  13. implementation 'org.springframework.boot:spring-boot-starter-data-neo4j'
  14. compile group: 'org.neo4j', name: 'neo4j-ogm-bolt-driver', version: '3.2.11'
  15. compile group: 'org.apache.commons', name: 'commons-lang3', version: '3.10'
  16. implementation 'org.springframework.boot:spring-boot-starter'
  17. testImplementation('org.springframework.boot:spring-boot-starter-test') {
  18. exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
  19. }
  20. }
  21. test {
  22. useJUnitPlatform()
  23. }

应用程序属性(application.properties):

  1. spring.data.neo4j.uri=bolt://localhost:7687
  2. spring.data.neo4j.username=neo4j
  3. 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.

  1. Caused by: org.neo4j.ogm.exception.core.MappingException: Error mapping GraphModel
  2. 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

  1. import org.neo4j.ogm.annotation.GeneratedValue;
  2. import org.neo4j.ogm.annotation.Id;
  3. import org.neo4j.ogm.annotation.NodeEntity;
  4. @NodeEntity
  5. public class LineItemStatus {
  6. @Id
  7. @GeneratedValue
  8. private Long id;
  9. private String name;
  10. private String description;
  11. private int rowStatusId;
  12. public LineItemStatus(String name, String description, int rowStatusId) {
  13. this.name = name;
  14. this.description = description;
  15. this.rowStatusId = rowStatusId;
  16. }
  17. public Long getId() {
  18. return id;
  19. }
  20. public void setId(Long id) {
  21. this.id = id;
  22. }
  23. public String getName() {
  24. return name;
  25. }
  26. public void setName(String name) {
  27. this.name = name;
  28. }
  29. public String getDescription() {
  30. return description;
  31. }
  32. public void setDescription(String description) {
  33. this.description = description;
  34. }
  35. public int getRowStatusId() {
  36. return rowStatusId;
  37. }
  38. public void setRowStatusId(int rowStatusId) {
  39. this.rowStatusId = rowStatusId;
  40. }
  41. }

reopsioty:

  1. import com.ns.tbe.model.nodes.LineItemStatus;
  2. import org.springframework.data.neo4j.repository.Neo4jRepository;
  3. import org.springframework.stereotype.Repository;
  4. @Repository
  5. public interface LineItemStatusRepository extends Neo4jRepository&lt;LineItemStatus, Long&gt; {
  6. LineItemStatus findByName(String name);
  7. }

Service:

  1. import org.springframework.beans.factory.annotation.Autowired;
  2. import org.springframework.stereotype.Service;
  3. @Service
  4. public class LineItemStatusService {
  5. @Autowired
  6. LineItemStatusRepository lineItemStatusRepository;
  7. public void saveLineItemStatus() {
  8. lineItemStatusRepository.save(new LineItemStatus(&quot;New&quot;, &quot;new&quot;, 1));
  9. lineItemStatusRepository.save(new LineItemStatus(&quot;Modified&quot;, &quot;modified&quot;, 1));
  10. lineItemStatusRepository.save(new LineItemStatus(&quot;Deleted&quot;, &quot;deleted&quot;, 1));
  11. LineItemStatus status = lineItemStatusRepository.findByName(&quot;New&quot;);
  12. }
  13. }

build.gradle file:

  1. plugins {
  2. id &#39;org.springframework.boot&#39; version &#39;2.3.0.RELEASE&#39;
  3. id &#39;io.spring.dependency-management&#39; version &#39;1.0.9.RELEASE&#39;
  4. id &#39;java&#39;
  5. }
  6. group = &#39;com.ns&#39;
  7. version = &#39;0.0.1-SNAPSHOT&#39;
  8. sourceCompatibility = &#39;11&#39;
  9. repositories {
  10. mavenCentral()
  11. }
  12. dependencies {
  13. implementation &#39;org.springframework.boot:spring-boot-starter-data-neo4j&#39;
  14. compile group: &#39;org.neo4j&#39;, name: &#39;neo4j-ogm-bolt-driver&#39;, version: &#39;3.2.11&#39;
  15. compile group: &#39;org.apache.commons&#39;, name: &#39;commons-lang3&#39;, version: &#39;3.10&#39;
  16. implementation &#39;org.springframework.boot:spring-boot-starter&#39;
  17. testImplementation(&#39;org.springframework.boot:spring-boot-starter-test&#39;) {
  18. exclude group: &#39;org.junit.vintage&#39;, module: &#39;junit-vintage-engine&#39;
  19. }
  20. }
  21. test {
  22. useJUnitPlatform()
  23. }

Application Properties:

  1. spring.data.neo4j.uri=bolt://localhost:7687
  2. spring.data.neo4j.username=neo4j
  3. 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.

huangapple
  • 本文由 发表于 2020年8月25日 22:03:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/63580609.html
匿名

发表评论

匿名网友

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

确定