findByPropertyAndRelation 没有给我预期的实体
findByPropertyAndReleation not giving me the expected Entity
我正在使用 spring 启动应用程序 (2.1.6.RELEASE) 将历史足球(或足球,如果你来自美国)数据导入 Neo4j 数据库,spring-boot-starter-data-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?
)
(我省略了国家 class,因为我很确定它不是问题的一部分)
为了允许 运行 多次导入,我想检查相应的实体是否已经存在于数据库中并且只导入较新的实体。所以我添加了以下方法 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}
).
我只是通过传递联盟的 id,并使用 @Query
注释提供自定义查询来让它工作,但我实际上认为,它可以与 spring-data 一起工作-neo4j 开箱即用。
感谢任何帮助。如果您需要更多详细信息,请告诉我。
Spring Data Neo4j 目前不支持对象作为参数。可以查询相关 entities/nodes 的属性,例如findBySeasonNumberAndLeagueName
如果这是一个合适的解决方案。
我正在使用 spring 启动应用程序 (2.1.6.RELEASE) 将历史足球(或足球,如果你来自美国)数据导入 Neo4j 数据库,spring-boot-starter-data-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?
)
(我省略了国家 class,因为我很确定它不是问题的一部分)
为了允许 运行 多次导入,我想检查相应的实体是否已经存在于数据库中并且只导入较新的实体。所以我添加了以下方法 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}
).
我只是通过传递联盟的 id,并使用 @Query
注释提供自定义查询来让它工作,但我实际上认为,它可以与 spring-data 一起工作-neo4j 开箱即用。
感谢任何帮助。如果您需要更多详细信息,请告诉我。
Spring Data Neo4j 目前不支持对象作为参数。可以查询相关 entities/nodes 的属性,例如findBySeasonNumberAndLeagueName
如果这是一个合适的解决方案。