【问题标题】:Spring boot Neo4j - query depth not working correctlySpring boot Neo4j - 查询深度无法正常工作
【发布时间】:2020-11-30 18:02:28
【问题描述】:

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

在我的带有 Neo4j 的 Spring Boot 项目中,我有 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 节点,它检索一级深度最大值并在叶节点上返回 null。

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

以下是我咨询过的资源列表以及包含完整代码的可用 GitHub 存储库:

最后的笔记:

  • 实体都有默认参数,因为 Kotlin 然后创建了一个空构造函数,我认为 OGM 需要它
  • 我也尝试过进行自定义查询,但无法指定深度值,因为存在不同的关系并且可以处于不同的级别
  • 要使用我链接的 GitHub 存储库,您必须安装 Neo4j,该存储库有一个包含所有代码的 stackoverflow-question 分支。

版本:

  • 弹簧启动:2.3.0.BUILD-SNAPSHOT
  • spring-boot-starter-data-neo4j: 2.3.0.BUILD-SNAPSHOT

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

【问题讨论】:

    标签: spring-boot kotlin neo4j spring-data-neo4j neo4j-ogm


    【解决方案1】:

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

    要查看发送到服务器的 Cypher 请求,您可以将 logging.level.org.neo4j.ogm.drivers.bolt=DEBUG 添加到 application.properties

    变更前的要求:

    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}
    

    变更后的要求:

    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}
    

    【讨论】:

    • 这是一个很好的收获!谢谢!有没有办法可以互换地从度量到微积分或价值?至少除了删除接口并仅在同一实体中组合 Calculus 和 Value 属性并在每种情况下设置空值之外。谢谢!你一直很有帮助,你应该得到赏金。
    • 你可以摆脱Value,并始终使用Calculus,方法是在Calculus定义中添加var value: Double? = null并修改其calculate()以返回value,如果它不是-null。
    猜你喜欢
    • 2021-11-02
    • 2020-02-09
    • 2019-01-23
    • 2021-11-10
    • 2018-09-23
    • 1970-01-01
    • 1970-01-01
    • 2015-04-20
    • 2016-09-16
    相关资源
    最近更新 更多