gpt4 book ai didi

java - 我如何使用 GraphRepositories 返回具有指定深度的复杂 POJO(当前使用 Neo4j 模板)

转载 作者:行者123 更新时间:2023-11-30 10:46:48 24 4
gpt4 key购买 nike

上下文:

我正在开发一个由 Neo4j 数据库支持的 java spring web 应用程序。我有一个对象“学生”,它已经学习了许多“模块”和“类(class)”。该关系通过指定成绩属性的“丰富关系”节点“RR_TakenModule”和“RR_TakenCourse”连接。

public class Student extends DomainObject {
String fullName;
Gender gender;
Collection<RR_TakenModule> modulesTaken;
Collection<RR_TakenCourse> coursesTaken;
DateTime yearStarted;

这样我就可以要求一个模块中获得 74% 的学生

问题:

我无法从 GraphRepository 设置中返回“深层”对象。即我无法返回具有填充的“modulesTaken”和“coursesTaken”属性的学生。我在网上看到了几种方法,例如尝试密码查询:

MATCH (student:Student) -[:MODULES_TAKEN]-> (rr:RR_TakenModule) -[:MODULE]-> (m:Module) RETURN student, COLLECT(rr) as modulesTaken

声称通过属性名称将 RR_TakenModules 动态映射到对象中。它不会为我执行此操作并返回“错误将 GraphModel 映射到 com.domain.Actors.Student 的实例”错误代码。尽管请注意,在 localhost:7474 界面中运行密码查询时它确实正确分组。显然映射是问题所在。

目前我采用的是模板+ map 的方式。使用 Neo4jOperations 和结果对象。这行得通,但是意味着我必须写出迭代器,这些迭代器会根据结果对象中的键/值对进行遍历和赋值。这会导致高维护成本和更大的错误几率。

环顾四周它们曾经是诸如 @Fetch 之类的选项,并在 Neo4jTemplate 查询中指定深度,但是这些方法似乎都没有出现在我的版本中(大多数似乎已被贬低)

问题:

有没有办法通过 Graph Repositories 映射 Neo4j 实体对象的子对象(即“collection prop”和“set prop”)。我知道有预定义的方法,例如具有深度参数的“findOne”,但我想对我的自定义查询应用类似的实现。

或者,是否有一种解决方案可以在不定义一些自定义 json 解析器的情况下将 Neo4jOperations Result 对象动态映射到 java 对象

版本:

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.3.1.RELEASE</version>
</parent>

<properties>
<java.version>1.8</java.version>
<spring-data-neo4j.version>4.0.0.RELEASE</spring-data-neo4j.version>
<spring-data-releasetrain.version>Gosling-RELEASE</spring-data-releasetrain.version>
</properties>

最佳答案

Spring Data Neo4j (SDN) 不会在返回的节点中填充引用的(按关系)节点。但是,它将填充 fullNamegender 属性(这里假设 Gender 是一个 Enum,这样一个字符串可以很容易地映射)。

在上面的查询中,您返回了一个 Student 节点和一个 RR_TakenModule 集合,但是 Spring 不知道如何映射它,因为没有包含这两个结果的类.您需要为这种情况定义一个明确的结果。我建议如下:

@NodeEntity(label = "Student")
public class Student extends DomainObject {
@Property(name = "studentId")
private String studentId; //you want to have this
@Property(name = "fullName")
private String fullName;
@Property(name = "gender")
private Gender gender; //assuming its an Enum
@Relationship(type = "MODULES_TAKEN") // Relationship.OUTGOING is default
private Set<RR_TakenModule) modulesTaken;
}

然后定义一个仓库

public interface StudentRepository extends GraphRepository<Student> {

@Query("MATCH (s:Student {studentId:{studentId}})-[:MODULES_TAKEN]->(m:RR_TakenModule) RETURN s AS student, collect(DISTINCT m) AS modulesTaken")
public StudentWithModulesTakenResult getByUserIdWithModulesTaken(@Param("studentId") String studentId)
}

和各自的结果

@QueryResult
public class StudentWithModulesTakenResult {
private Student student;
private Set<RR_TakenModule> modulesTaken;
//only getters, no setters
}

关于java - 我如何使用 GraphRepositories 返回具有指定深度的复杂 POJO(当前使用 Neo4j 模板),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36367916/

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