NoSuchRecordException when saving Entity on Neo4j

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

NoSuchRecordException when saving Entity on Neo4j

问题

I am able to save an entity on my local PC using Neo4j Desktop version "5.3.0" but when I have the same exact same code and try saving the entity on the remote server with older version of "4.2.5, I am getting the below error;

2023-05-11 09:48:13.525 WARN 90586 --- [ poolThread-96] .n.c.Neo4jPersistenceExceptionTranslator : Don't know how to translate exception of type class org.neo4j.driver.exceptions.NoSuchRecordException
Exception in thread "poolThread-96" org.neo4j.driver.exceptions.NoSuchRecordException: Expected a result with a single record, but this result contains at least one more. Ensure your query returns only one record.

Repository

@Repository

public interface OntologyRepository extends Neo4jRepository<Ontology, String> {
}

Service

@Service
public class OntologyService {
@Autowired
private OntologyRepository ontologyRepository;

  1. public void saveProfile(Ontology ontology) {
  2. ontologyRepository.save(ontology);
  3. }

}

Entity

@Data
@EqualsAndHashCode(callSuper = false)
@JsonIgnoreProperties(ignoreUnknown = true)
@Node("Profile")
@Component
public class Ontology extends Profile implements Cloneable {

  1. @Id
  2. private String hash;
  3. @Transient
  4. private long id;
  5. //etc.

}

In the Service class, If the return type was also the object that I am trying to save, I would understand the error but that is not the case. I am just trying to save without querying any data. Any help would be great. Thanks

英文:

I am able to save an entity on my local PC using Neo4j Desktop version "5.3.0" but when I have the same exact same code and try saving the entity on the remote server with older version of "4.2.5, I am getting the below error;

  1. 2023-05-11 09:48:13.525 WARN 90586 --- [ poolThread-96] .n.c.Neo4jPersistenceExceptionTranslator : Don&#39;t know how to translate exception of type class org.neo4j.driver.exceptions.NoSuchRecordException
  2. Exception in thread &quot;poolThread-96&quot; org.neo4j.driver.exceptions.NoSuchRecordException: Expected a result with a single record, but this result contains at least one more. Ensure your query returns only one record.

Repository

  1. @Repository
  2. public interface OntologyRepository extends Neo4jRepository&lt;Ontology, String&gt; {
  3. }

Service

  1. @Service
  2. public class OntologyService {
  3. @Autowired
  4. private OntologyRepository ontologyRepository;
  5. public void saveProfile(Ontology ontology) {
  6. ontologyRepository.save(ontology);
  7. }
  8. }

Entity

  1. @Data
  2. @EqualsAndHashCode(callSuper = false)
  3. @JsonIgnoreProperties(ignoreUnknown = true)
  4. @Node(&quot;Profile&quot;)
  5. @Component
  6. public class Ontology extends Profile implements Cloneable {
  7. @Id
  8. private String hash;
  9. @Transient
  10. private long id;
  11. //etc.
  12. }

In the Service class, If the return type was also the object that I am trying to save, I would understand the error but that is not the case. I am just trying to save without querying any data. Any help would be great. Thanks

答案1

得分: 2

The error message Expected a result with a single record, but this result contains at least one more. Ensure your query returns only one record. states that there is more than one record, with the same primary key. I believe this is due to your hash key not being unique across nodes, as it is marked with @Id. If you are not assigning hash manually, then you should auto-generate it like this:

  1. @Data
  2. @EqualsAndHashCode(callSuper = false)
  3. @JsonIgnoreProperties(ignoreUnknown = true)
  4. @Node("Profile")
  5. @Component
  6. public class Ontology extends Profile implements Cloneable {
  7. @Id
  8. @GeneratedValue(generatorClass = UUIDStringGenerator.class)
  9. private String hash;
  10. @Transient
  11. private long id;
  12. //etc.
  13. }

This should resolve the error.

英文:

The error message Expected a result with a single record, but this result contains at least one more. Ensure your query returns only one record., states that there is more than one record, with the same primary key. I believe this is due to your hash key not being unique across nodes, as it is marked with @Id. If you are not assigning hash manually, then you should auto-generate it like this:

  1. @Data
  2. @EqualsAndHashCode(callSuper = false)
  3. @JsonIgnoreProperties(ignoreUnknown = true)
  4. @Node(&quot;Profile&quot;)
  5. @Component
  6. public class Ontology extends Profile implements Cloneable {
  7. @Id
  8. @GeneratedValue(generatorClass = UUIDStringGenerator.class)
  9. private String hash;
  10. @Transient
  11. private long id;
  12. //etc.
  13. }

This should resolve the error.

huangapple
  • 本文由 发表于 2023年5月11日 19:13:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/76226996.html
匿名

发表评论

匿名网友

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

确定