Spring 启动 Neo4j - 查询深度无法正常工作

Spring boot Neo4j - query depth not working correctly

TL;DR: @Depth(value = -1) 抛出空指针,其他大于 1 的值被忽略

在我的 Spring Boot with Neo4j 项目中,我有 3 个具有关系的简单实体:

@NodeEntity
data class Metric(
        @Id @GeneratedValue val id: Long = -1,
        val name: String = "",
        val description: String = "",
        @Relationship(type = "CALCULATES")
        val calculates: MutableSet<Calculable> = mutableSetOf()
) {
    fun calculates(calculable: Calculus) = calculates.add(calculable)
    fun calculate() = calculates.map { c -> c.calculate() }.sum()
}

interface Calculable {
    fun calculate(): Double
}

@NodeEntity
data class Calculus(
        @Id @GeneratedValue val id: Long = -1,
        val name: String = "",
        @Relationship(type = "LEFT")
        var left: Calculable? = null,
        @Relationship(type = "RIGHT")
        var right: Calculable? = null,
        var operator: Operator? = null
) : Calculable {
    override fun calculate(): Double =
            operator!!.apply(left!!.calculate(), right!!.calculate())
}

@NodeEntity
data class Value(
        @Id @GeneratedValue val id: Long = -1,
        val name: String = "",
        var value: Double = 0.0
) : Calculable {
    override fun calculate(): Double = value
}

enum class Operator : BinaryOperator<Double>, DoubleBinaryOperator {//not relevant}

我创建了一个像这样的简单图表:

具有以下存储库:

@Repository
interface MetricRepository : Neo4jRepository<Metric, Long>{
    @Depth(value = 2)
    fun findByName(name: String): Metric?
}

@Repository
interface CalculusRepository : Neo4jRepository<Calculus, Long>{
    fun findByName(name: String): Calculus?
}

@Repository
interface ValueRepository : Neo4jRepository<Value, Long>{
    fun findByName(name: String): Value?
}

以及以下代码:

// calculus
val five = valueRepository.save(Value(
        name = "5",
        value = 5.0
))

val two = valueRepository.save(Value(
        name = "2",
        value = 2.0
))

val fiveTimesTwo = calculusRepository.save(Calculus(
        name = "5 * 2",
        operator = Operator.TIMES,
        left = five,
        right = two
))

println("---")
println(fiveTimesTwo)
val fromRepository = calculusRepository.findByName("5 * 2")!!
println(fromRepository) // sometimes has different id than fiveTimesTwo
println("5 * 2 = ${fromRepository.calculate()}")
println("--- \n")


// metric
val metric = metricRepository.save(Metric(
        name = "Metric 1",
        description = "Measures a calculus",
        calculates = mutableSetOf(fromRepository)
))
metricRepository.save(metric)

println("---")
println(metric)
val metricFromRepository = metricRepository.findByName("Metric 1")!!
println(metricFromRepository) // calculates node is partially empty
println("--- \n")

要检索如上图所示的相同图表(取自实际的 neo4j 仪表板),我执行 metricRepository.findByName("Metric 1"),其中包含 @Depth(value = 2),然后打印保存的指标和检索的指标:

Metric(id=9, name=Metric 1, description=Measures a calculus, calculates=[Calculus(id=2, name=5 * 2, left=Value(id=18, name=5, value=5.0), right=Value(id=1, name=2, value=2.0), operator=TIMES)])

Metric(id=9, name=Metric 1, description=Measures a calculus, calculates=[Calculus(id=2, name=5 * 2, left=null, right=null, operator=TIMES)])

无论深度的值如何,我都无法获取 Metric 节点及其所有子节点,它会在叶节点上检索一级深度 max 和 returns null。

我在文档中读到 depth=-1 检索完全解析的节点,但这样做会导致 findByName() 方法失败并返回空指针:kotlin.KotlinNullPointerException: null

这是我查阅过的资源列表和一个包含完整代码的工作 GitHub 存储库:

最后的笔记:

版本:

感谢您的帮助,欢迎所有反馈!

问题不在于查询深度,而在于模型。 Metric 实体与 Calculable 有关系,但 Calculable 本身没有定义任何关系。 Spring 数据无法扫描 Calculable 接口的所有可能实现以了解它们之间的关系。如果您将 Metrics.calculates 类型更改为 MutableSet<Calculus>,它将按预期工作。

To see Cypher requests send to the server you can add logging.level.org.neo4j.ogm.drivers.bolt=DEBUG to the application.properties

Request before the change:

MATCH (n:`Metric`) WHERE n.`name` = $`name_0` WITH n RETURN n,[ [ (n)->[r_c1:`CALCULATES`]->(x1) | [ r_c1, x1 ] ] ], ID(n) with params {name_0=Metric 1}

Request after the change:

MATCH (n:`Metric`) WHERE n.`name` = $`name_0` WITH n RETURN n,[ [ (n)->[r_c1:`CALCULATES`]->(c1:`Calculus`) | [ r_c1, c1, [ [ (c1)-[r_l2:`LEFT`]-(v2:`Value`) | [ r_l2, v2 ] ], [ (c1)-[r_r2:`RIGHT`]-(v2:`Value`) | [ r_r2, v2 ] ] ] ] ] ], ID(n) with params {name_0=Metric 1}