通过对象搜索 属性

Search via object property

我有一个 SidebarBox class,其中有一个人 属性。我想根据这个人获取所有 SidebarBoxes。 为此,我创建了以下存储库:

public interface SidebarRepository extends Neo4jRepository<SidebarBox, Long> {
    List<SidebarBox> findAllByPerson_Id(Long personId);
    List<SidebarBox> findAllByPerson(Person person);
}

如果我通过 findAllByPerson_Id 搜索,我根本找不到条目,​​如果我通过 findAllByPerson 搜索,程序会崩溃并显示以下错误消息:

java.lang.IllegalStateException: Failed to execute CommandLineRunner
    at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:807) ~[spring-boot-2.4.2.jar:2.4.2]
    at org.springframework.boot.SpringApplication.callRunners(SpringApplication.java:788) ~[spring-boot-2.4.2.jar:2.4.2]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:333) ~[spring-boot-2.4.2.jar:2.4.2]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1311) ~[spring-boot-2.4.2.jar:2.4.2]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1300) ~[spring-boot-2.4.2.jar:2.4.2]
    at de.skillkiller.persondb.PersondbApplication.main(PersondbApplication.java:24) ~[classes/:na]
Caused by: org.springframework.data.mapping.MappingException: The property 'null' is not mapped to a Graph property!
    at org.springframework.data.neo4j.core.mapping.DefaultNeo4jPersistentProperty.getPropertyName(DefaultNeo4jPersistentProperty.java:247) ~[spring-data-neo4j-6.0.3.jar:6.0.3]
    at org.springframework.data.neo4j.repository.query.CypherQueryCreator.toCypherProperty(CypherQueryCreator.java:549) ~[spring-data-neo4j-6.0.3.jar:6.0.3]
    at org.springframework.data.neo4j.repository.query.CypherQueryCreator.createImpl(CypherQueryCreator.java:385) ~[spring-data-neo4j-6.0.3.jar:6.0.3]
    at org.springframework.data.neo4j.repository.query.CypherQueryCreator.create(CypherQueryCreator.java:240) ~[spring-data-neo4j-6.0.3.jar:6.0.3]
    at org.springframework.data.neo4j.repository.query.CypherQueryCreator.create(CypherQueryCreator.java:85) ~[spring-data-neo4j-6.0.3.jar:6.0.3]
    at org.springframework.data.repository.query.parser.AbstractQueryCreator.createCriteria(AbstractQueryCreator.java:119) ~[spring-data-commons-2.4.3.jar:2.4.3]
    at org.springframework.data.repository.query.parser.AbstractQueryCreator.createQuery(AbstractQueryCreator.java:95) ~[spring-data-commons-2.4.3.jar:2.4.3]
    at org.springframework.data.repository.query.parser.AbstractQueryCreator.createQuery(AbstractQueryCreator.java:81) ~[spring-data-commons-2.4.3.jar:2.4.3]
    at org.springframework.data.neo4j.repository.query.PartTreeNeo4jQuery.prepareQuery(PartTreeNeo4jQuery.java:69) ~[spring-data-neo4j-6.0.3.jar:6.0.3]
    at org.springframework.data.neo4j.repository.query.AbstractNeo4jQuery.execute(AbstractNeo4jQuery.java:82) ~[spring-data-neo4j-6.0.3.jar:6.0.3]
    at org.springframework.data.repository.core.support.RepositoryMethodInvoker.doInvoke(RepositoryMethodInvoker.java:137) ~[spring-data-commons-2.4.3.jar:2.4.3]
    at org.springframework.data.repository.core.support.RepositoryMethodInvoker.invoke(RepositoryMethodInvoker.java:121) ~[spring-data-commons-2.4.3.jar:2.4.3]
    at org.springframework.data.repository.core.support.QueryExecutorMethodInterceptor.doInvoke(QueryExecutorMethodInterceptor.java:152) ~[spring-data-commons-2.4.3.jar:2.4.3]
    at org.springframework.data.repository.core.support.QueryExecutorMethodInterceptor.invoke(QueryExecutorMethodInterceptor.java:131) ~[spring-data-commons-2.4.3.jar:2.4.3]
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) ~[spring-aop-5.3.3.jar:5.3.3]
    at org.springframework.data.projection.DefaultMethodInvokingMethodInterceptor.invoke(DefaultMethodInvokingMethodInterceptor.java:80) ~[spring-data-commons-2.4.3.jar:2.4.3]
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) ~[spring-aop-5.3.3.jar:5.3.3]
    at org.springframework.transaction.interceptor.TransactionInterceptor.proceedWithInvocation(TransactionInterceptor.java:123) ~[spring-tx-5.3.3.jar:5.3.3]
    at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:388) ~[spring-tx-5.3.3.jar:5.3.3]
    at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:119) ~[spring-tx-5.3.3.jar:5.3.3]
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) ~[spring-aop-5.3.3.jar:5.3.3]
    at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:137) ~[spring-tx-5.3.3.jar:5.3.3]
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) ~[spring-aop-5.3.3.jar:5.3.3]
    at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:97) ~[spring-aop-5.3.3.jar:5.3.3]
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) ~[spring-aop-5.3.3.jar:5.3.3]
    at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:215) ~[spring-aop-5.3.3.jar:5.3.3]
    at com.sun.proxy.$Proxy93.findAllByPerson(Unknown Source) ~[na:na]
    at de.skillkiller.persondb.PersondbApplication.lambda$demo[=12=](PersondbApplication.java:50) ~[classes/:na]
    at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:804) ~[spring-boot-2.4.2.jar:2.4.2]
    ... 5 common frames omitted

2021-02-23 18:51:36.308  INFO 34603 --- [           main] o.s.s.concurrent.ThreadPoolTaskExecutor  : Shutting down ExecutorService 'applicationTaskExecutor'
2021-02-23 18:51:36.311  INFO 34603 --- [           main] org.neo4j.driver.Driver                  : Closing driver instance 1555928242
2021-02-23 18:51:36.313  INFO 34603 --- [           main] org.neo4j.driver.ConnectionPool          : Closing connection pool towards localhost:7687
Disconnected from the target VM, address: '127.0.0.1:40261', transport: 'socket'

Process finished with exit code 1

CommandLineRunner(简称):

            Person person = new Person();
            person.setFirstname("Dieter");
            person.setLastname("Zufall");

            personRepository.save(person);

            SidebarBox sidebarBox = new SidebarBox("Test", person, new ArrayList<>());
            sidebarBox.addInformation(new SidebarBoxEntry("Key", "Nein"));
            sidebarBox.addInformation(new SidebarBoxEntry("AKey", "Nein"));

            sidebarRepository.save(sidebarBox);
            List<SidebarBox> a = sidebarRepository.findAll();
            List<SidebarBox> b = sidebarRepository.findAllByPerson_Id(person.getId());
            List<SidebarBox> c = sidebarRepository.findAllByPerson(person);

人:

@EqualsAndHashCode(callSuper = true)
@Node
@Data
@ToString(callSuper = true)
@NoArgsConstructor
@AllArgsConstructor
public class Person extends BasicNode {
    private String firstname;
    private String lastname;
    private String comment;
}

侧边栏框:

@EqualsAndHashCode(callSuper = true)
@Node
@Data
@ToString(callSuper = true)
@NoArgsConstructor
@AllArgsConstructor
public class SidebarBox extends BasicNode{
    private String title;
    private Person person;
    private List<SidebarBoxEntry> information = new ArrayList<>();

    public void addInformation(SidebarBoxEntry entry) {
        information.add(entry);
    }

    public void removeInformation(SidebarBoxEntry entry) {
        information.remove(entry);
    }
}

基本节点:

@Data
@AllArgsConstructor
@NoArgsConstructor
public abstract class BasicNode {
    @Id
    @GeneratedValue
    private Long id;

    @DateLong
    @LastModifiedDate
    private Date changedAt = new Date();

    @DateLong
    @CreatedDate
    private Date enteredAt = new Date();
}

很遗憾,我不知道如何修复此错误。 你能告诉我如何实现这样的查询吗?

由于与 BasicNode 的继承,Spring Data Neo4j 中存在一个错误,该错误采用了错误的元信息,在您的情况下没有创建正确的类型(SidebarBox). 本月晚些时候将提供更新。同时,您可以检查 SDN 的 6.0.6-SNAPSHOT 版本。 我用你的代码创建了一个例子 https://github.com/meistermeier/neo4j-issues-examples/tree/master/relatedIdQuery 使用这个版本,它适用于 List<SidebarBox> findAllByPerson_Id(Long personId)

对于即将发布的 6.1 版本,我们计划引入“按对象查找”功能,以提供更大的灵活性。