NoSuchRecordException when saving Entity on Neo4j

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

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;

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

}

Entity

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

@Id
private String hash;

@Transient
private long id;

//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;

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
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

@Repository

public interface OntologyRepository extends Neo4jRepository&lt;Ontology, String&gt; {
}

Service

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

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

Entity

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

    @Id
    private String hash;

    @Transient
    private long id;

    //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

答案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:

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

    @Id
    @GeneratedValue(generatorClass = UUIDStringGenerator.class)
    private String hash;

    @Transient
    private long id;

    //etc.
}

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:

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

    @Id
    @GeneratedValue(generatorClass = UUIDStringGenerator.class)
    private String hash;

    @Transient
    private long id;

    //etc.
}

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:

确定