Spring数据neo4j不初始化用@Relationship注释的对象

问题描述 投票:0回答:1

我有这样的位置模型(呈现专有模型的等效模拟,省略自动生成的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
}

SubLocation是几个不同具体类的摘要,如OfficeDeskRoom等,这些实现是无关紧要的。使用Location,例如在Person班:

@NodeEntity
class Person(
    var name: String,
    @JsonIgnore @Relationship(type = "WORKS_IN", direction = Relationship.OUTGOING)
    var location: Location? = null
)

当我有像Person-> RoomSubLocation) - > Building这样的子图时,一切都像魅力一样。我通过Neo4jRepository界面查询它产生一个Person对象与locationPerson-> SubLocation)以及locatedInSubLocation-> Building)正确设置:

interface PersonRepository: Neo4jRepository<Person, Long> {
    @Depth(5) // exaggerated for test purposes
    fun findAllByName(name: String): List<Person>
}

当我有一个子图时,问题就会显露出来

Person-> Space - > RoomSubLocation) - > Building

当我使用相同的存储库方法查询时,我只获得映射到对象的第一级关系。 Person对象有location设置为Space,但SpacesubLocation设置为null

我使用的是最新版本:spring-data-neo4j -> 5.1.6.RELEASEneo4j-ogm-core -> 3.1.8以及neo4j:3.5.3

TL; DR:

spring-data-neo4j不会自动将具有抽象类类型的@Relationship注释字段映射到具体对象,null被分配。

kotlin neo4j spring-data-neo4j neo4j-ogm
1个回答
0
投票

显然它可以通过自定义@Query解决:

interface PersonRepository: Neo4jRepository<Person, Long> {
    @Query(""""
        MATCH g=(:Person)-[*1..3]->(:Building)
        RETURN g
    """")
    fun findAllByName(name: String): List<Person>
}
© www.soinside.com 2019 - 2024. All rights reserved.