gpt4 book ai didi

java - Neo4j 返回问题 : How to map the properties of one node and one property from other node which connected into java object

转载 作者:行者123 更新时间:2023-12-02 01:32:55 25 4
gpt4 key购买 nike

我在将 Neo4j 返回的属性映射到 java 时遇到一些问题。

我有两个连接的节点,Group(g)和Tenant(t),返回的值为g,t.id。

在java端,我有一个组对象,但它仅映射组而不是tenantId。

在组对象中,tenantId 的类型很长,如果我将其更改为租户对象并返回完整的租户及其关系,它也会映射租户,但我希望该组仅包含 id 和不是整个租户。

@NodeEntity(label = "Group")
public class GroupEntity extends AbstractBaseEntity {

public static final String ENTITY_TYPE = "Group";

@Property(name="name")
private String name;
@Property(name="description")
private String description;

@Relationship(type = "MEMBER_OF")
private long tenantId;
}

//The query

@Query("MATCH (g:Group) where g.id = {groupId} " +
"optional match (g)-[:MEMBER_OF]->(t:Tenant) " +
"return g,t.id as tenantId")
Optional<GroupEntity> findById(Long groupId);

最佳答案

它不是这样工作的。

您在实体之间进行映射。

但是对于查询,您可以返回任何形状的数据并将其映射到代表您需求的 DTO。

例如

@Query("MATCH (g:Group) where g.id = {groupId} " +
"optional match (g)-[:MEMBER_OF]->(t:Tenant) " +
"return g, collect(t.id) as tenantIds")
Optional<GroupWithTenantIds> findById(Long groupId);

@QueryResult
static class GroupWithTenantIds {
Group group;
Set<Long> tenantIds;
}

关于java - Neo4j 返回问题 : How to map the properties of one node and one property from other node which connected into java object,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57545587/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com