Spring数据neo4j不会初始化带有@Relationship注释的对象

时间:2019-04-11 15:54:43

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

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

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

我正在使用spring-data-neo4j -> 5.1.6.RELEASEneo4j-ogm-core -> 3.1.8neo4j:3.5.3的最新版本。

TL; DR:

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

1 个答案:

答案 0 :(得分:0)

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

interface PersonRepository: Neo4jRepository<Person, Long> {
    @Query(""""
        MATCH g=(:Person)-[*1..3]->(:Building)
        RETURN g
    """")
    fun findAllByName(name: String): List<Person>
}