spring-boot - Spring boot Neo4j - 查询深度无法正常工作
问题描述
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
感谢您的帮助,欢迎所有反馈!
解决方案
问题不在于查询深度,而在于模型。实体与Metric
有关系Calculable
,但Calculable
它本身没有定义关系。Spring Data 无法扫描Calculable
接口的所有可能实现以了解它们之间的关系。如果您将Metrics.calculates
type 更改为MutableSet<Calculus>
,它将按预期工作。
要查看发送到服务器的 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}
推荐阅读
- python - python列表中是否有任何函数可以将逗号分隔值转换为单个元素?
- slack - 如何不在松弛中格式化表情符号?
- postgresql - 将 Microstrategy Cube 连接到 Postgres 物化视图
- python-2.7 - 如何为此元素编写定位器
- javascript - 涉及带条件数组的 Switch 语句中的案例
- android - Android 在 sdk 集成中链接
- php - 按添加时间对mysql行进行编号,然后在保留数字的同时进行排序
- ruby-on-rails - 在 rake 任务期间无法执行原始 sql 'UPDATE ...'
- javascript - 同时使用的两种排序/过滤方法 Javascript
- java - Java ArrayList 删除元素恢复浪费的空间?