spring neo4j 使用自定义查询水合相关节点列表的问题

问题描述 投票:0回答:3

我使用 @Query 注释的自定义查询不会对每个父节点返回的相关节点进行水化。

实体

public class Dater implements CSVFormat{
    @Id
    private String userId;
    private String gender;
    private String postalCode;
    private int age;
    @Relationship(type = "LISTENS_TO")
    //polymorphic set pointing to various interface implementations
    private Set<PersistentDaterMusicItem> musicItems = new HashSet<>();
    ...//partially left out for brevity
}

查询

@Query("MATCH (user:Dater { userId:  $userId })-[:LISTENS_TO]->(musicItems)<-[mr:LISTENS_TO]-(matches:Dater) "
            + "where id(user) <> id(matches) "
            + "RETURN matches, collect(mr), collect(musicItems) ")
    List<Dater> getMatches(String userId);

来电者

List<Dater> daters = daterRepository.getMatches(id);

daters 列表包含 2 个 Dater 对象,这是正确的,但 musicItems 字段有 0 个项目。当执行直接返回每个父 Dater 节点的多个关系和相关节点时,cypher 查询工作正常。

Spring Neo4j 版本:

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-neo4j</artifactId>
            <version>2.7.4</version>
        </dependency>
neo4j spring-data-neo4j
3个回答
0
投票

问题是我正在尝试填充 PersistentDaterMusicItem 的列表,它是一个接口,我分配给 neo4j db 中的节点的标签是 PersistentDaterMusicItem 具体实现的类名 像歌曲、艺术家等。由于这些不匹配,因此没有水合作用。


0
投票

您是正确的,它没有映射应用程序实体,因为它在数据库中找不到任何可将 PersistentDaterMusicItem 映射到的内容。由于该实体是特定项目的抽象,因此您可以在应用程序中为“歌曲”、“艺术家”等创建子实体,并让每个子实体扩展 PersistentDaterMusicItem 类。这应该有助于 Spring 知道要在数据库中映射什么。我在 Java 版本上的 代码存储库 中为 Delta 类进行了类似的设置,该类可以是枚举、注释、字段等。


0
投票

如果您为扩展 PersistentDaterMusicItem 的歌曲、艺术家等创建单独的域类,它应该能够将它们映射回来。这不行吗?

© www.soinside.com 2019 - 2024. All rights reserved.