Spring data neo4j 不初始化用@Relationship 注解的对象
Spring data neo4j does not initialize objects annotated with @Relationship
我有一个这样的位置模型(提供专有模型的等效模拟、省略的自动生成的 ID 和一些其他字段):
@NodeEntity
class Space: Location() {
@field:Relationship(type = "SUBLOCATED_IN", direction = Relationship.OUTGOING) var subLocation: SubLocation? = null
}
@NodeEntity
abstract class SubLocation: Location() {
@field:Relationship(type = "LOCATED_IN", direction = Relationship.OUTGOING) var locatedIn: Building? = null
}
@NodeEntity
class Building: Location()
@NodeEntity
abstract class Location {
var name: String? = null
var city: String? = null
var country: String? = null
}
A SubLocation
是一些不同的具体 class 的抽象,例如 Office
、Desk
、Room
等,其实现无关紧要. Location
用于例如在 Person
class:
@NodeEntity
class Person(
var name: String,
@JsonIgnore @Relationship(type = "WORKS_IN", direction = Relationship.OUTGOING)
var location: Location? = null
)
当我有一个像 Person
->Room
(SubLocation
)->Building
这样的子图时,一切都很顺利。我正在通过 Neo4jRepository
接口进行查询,它会生成一个 Person
对象 location
(Person
->SubLocation
) 以及 locatedIn
(SubLocation
->Building
) 正确设置:
interface PersonRepository: Neo4jRepository<Person, Long> {
@Depth(5) // exaggerated for test purposes
fun findAllByName(name: String): List<Person>
}
当我有一个像
这样的子图时,问题就暴露出来了
Person
->Space
->Room
(SubLocation
)->Building
并且当我使用相同的存储库方法进行查询时,我只得到映射到对象的第一级关系。 Person
对象已将 location
正确设置为 Space
,但 Space
已将 subLocation
设置为 null
。
我使用的是最新版本:spring-data-neo4j -> 5.1.6.RELEASE
和 neo4j-ogm-core -> 3.1.8
以及 neo4j:3.5.3
。
TL;DR:
spring-data-neo4j
不会自动映射 @Relationship
带有抽象 class 类型的注释字段到具体对象,null
被赋值。
显然可以通过自定义解决 @Query
:
interface PersonRepository: Neo4jRepository<Person, Long> {
@Query(""""
MATCH g=(:Person)-[*1..3]->(:Building)
RETURN g
"""")
fun findAllByName(name: String): List<Person>
}
我有一个这样的位置模型(提供专有模型的等效模拟、省略的自动生成的 ID 和一些其他字段):
@NodeEntity
class Space: Location() {
@field:Relationship(type = "SUBLOCATED_IN", direction = Relationship.OUTGOING) var subLocation: SubLocation? = null
}
@NodeEntity
abstract class SubLocation: Location() {
@field:Relationship(type = "LOCATED_IN", direction = Relationship.OUTGOING) var locatedIn: Building? = null
}
@NodeEntity
class Building: Location()
@NodeEntity
abstract class Location {
var name: String? = null
var city: String? = null
var country: String? = null
}
A SubLocation
是一些不同的具体 class 的抽象,例如 Office
、Desk
、Room
等,其实现无关紧要. Location
用于例如在 Person
class:
@NodeEntity
class Person(
var name: String,
@JsonIgnore @Relationship(type = "WORKS_IN", direction = Relationship.OUTGOING)
var location: Location? = null
)
当我有一个像 Person
->Room
(SubLocation
)->Building
这样的子图时,一切都很顺利。我正在通过 Neo4jRepository
接口进行查询,它会生成一个 Person
对象 location
(Person
->SubLocation
) 以及 locatedIn
(SubLocation
->Building
) 正确设置:
interface PersonRepository: Neo4jRepository<Person, Long> {
@Depth(5) // exaggerated for test purposes
fun findAllByName(name: String): List<Person>
}
当我有一个像
这样的子图时,问题就暴露出来了Person
->Space
->Room
(SubLocation
)->Building
并且当我使用相同的存储库方法进行查询时,我只得到映射到对象的第一级关系。 Person
对象已将 location
正确设置为 Space
,但 Space
已将 subLocation
设置为 null
。
我使用的是最新版本:spring-data-neo4j -> 5.1.6.RELEASE
和 neo4j-ogm-core -> 3.1.8
以及 neo4j:3.5.3
。
TL;DR:
spring-data-neo4j
不会自动映射 @Relationship
带有抽象 class 类型的注释字段到具体对象,null
被赋值。
显然可以通过自定义解决 @Query
:
interface PersonRepository: Neo4jRepository<Person, Long> {
@Query(""""
MATCH g=(:Person)-[*1..3]->(:Building)
RETURN g
"""")
fun findAllByName(name: String): List<Person>
}