【问题标题】:findByPropertyAndReleation not giving me the expected EntityfindByPropertyAndReleation 没有给我预期的实体
【发布时间】:2019-11-06 07:15:51
【问题描述】:

我正在使用带有 spring-boot-starter-data-neo4j 依赖项和独立的本地运行的 Spring Boot 应用程序 (2.1.6.RELEASE) 将历史足球(或足球,如果您来自美国)数据导入 Neo4j 数据库3.5.6 Neo4j 数据库服务器。

但由于某种原因,通过简单属性和附加的引用实体搜索实体不起作用,尽管该关系存在于数据库中。

这是模型的一部分,目前让我很头疼:

@NodeEntity(label = "Season")
open class Season(
    @Id
    @GeneratedValue
    var id: Long? = null,

    @Index(unique = true)
    var name: String,

    var seasonNumber: Long,

    @Relationship(type = "IN_LEAGUE", direction = Relationship.OUTGOING)
    var league: League?,

    var start: LocalDate,

    var end: LocalDate
)

@NodeEntity(label = "League")
open class League(
    @Id
    @GeneratedValue
    var id: Long? = null,

    @Index(unique = true)
    var name: String,

    @Relationship(type = "BELONGS_TO", direction = Relationship.OUTGOING)
    var country: Country?
)

(我省略了 Country 类,因为我很确定它不是问题的一部分)

为了允许多次运行导入,我想检查相应的实体是否已经存在于数据库中,并且只导入较新的实体。所以我添加了以下方法SeasonRepository:

open class SeasonRepository : CrudRepository<Season, Long> {
    fun findBySeasonNumberAndLeague(number: Long, league: League): Season?
}

但它给我一个null 结果而不是连续运行的现有实体,因此我在我的数据库中得到重复。

我本来希望 spring-data-neo4j 将传递的 League 减少到它的 Id,然后生成一个看起来像这样的查询:

MATCH (s:Season)-[:IN_LEAGUE]->(l:League) WHERE id(l) = {leagueId} AND s.seasonNumber = {seasonNumber} WITH s MATCH (s)-[r]->(o) RETURN s,r,o

但是当我在 neo4j 包上打开更精细的日志记录时,我会在日志文件中看到以下输出:

MATCH (n:`Season`) WHERE n.`seasonNumber` = { `seasonNumber_0` } AND n.`league` = { `league_1` } WITH n RETURN n,[ [ (n)-[r_i1:`IN_LEAGUE`]->(l1:`League`) | [ r_i1, l1 ] ] ], ID(n) with params {league_1={id=30228, name=1. Bundesliga, country={id=29773, name=Deutschland}}, seasonNumber_0=1}

所以出于某种原因,spring-data 似乎认为,联赛属性是一个简单/原始的属性,而不是一个完整的关系,需要通过 id 来解决(n.league= {league_1@987654330 @)。

我只是通过传递联赛的 id 并使用 @Query 注释提供自定义查询,但实际上我认为它可以与开箱即用的 spring-data-neo4j 一起使用。

任何帮助表示赞赏。如果您需要更多详细信息,请告诉我。

【问题讨论】:

  • 你能试试只用 findByLeague 的方法,看看是否可行?
  • @SimonMartinelli 我添加了fun findByLeague(league : League) : List&lt;Season&gt; 方法,但它给了我一个空列表。运行查询 MATCH (s:Season)-[:IN_LEAGUE]-&gt;(l:League) WHERE id(l) = 31214 RETURN s.name 给了我 56 个结果(我从调试器中复制了联赛的 id 31214)。再次在日志中,似乎 spring-data 试图将联赛解析为简单属性:查询与原始问题中的完全相同,除了 seasonNumber 部分被省略:(

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


【解决方案1】:

Spring Data Neo4j 目前不支持对象作为参数。可以查询相关实体/节点的属性,例如findBySeasonNumberAndLeagueName 如果这是一个合适的解决方案。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-07
    相关资源
    最近更新 更多