java - Spring数据JDBC : Required identifier property not found for a MappedCollection

标签 java mysql kotlin spring-data spring-data-jdbc

class Game(
    @MappedCollection(idColumn = "game")         <---- unnecessary
    var rules: List<Rule> = emptyList(),
){
    @Id
    var id: Long? = null
}

data class Rule(
    @MappedCollection(idColumn = "rule", keyColumn = "rule_key")<---- unnecessary
    val ruleValues: List<RuleValue>
) 

data class RuleValue(val value: String)

架构是

create table game
(
    id              serial primary key
);

create table rule
(
    id              serial primary key,
    game            long references game (id),
    game_key        integer
);

create table rule_value
(
    id               serial primary key,
    game             long references game (id),
    game_key         integer,
    rule             long references rule (id),
    rule_key         integer,
    value            varchar(256)
);

简而言之,我有一个 Game 作为根聚合。 游戏有一个规则列表(顺序很重要),规则有一个规则值列表(顺序很重要) 规则值目前只是一个字符串,但将来可能会扩展。

我有 2 个问题:

1) 未找到所需的标识符属性:当我尝试保存游戏时,我得到 Caused by: java.lang.IllegalStateException: 未找到类 com 所需的标识符属性...规则值! 异常消息。

2) java.sql.SQLSyntaxErrorException:为什么我需要 rule_value 中的 gamegame_key 列 table ?如果我不放置它们,我会得到 Caused by: java.sql.SQLSyntaxErrorException: Unknown column 'game_key' in 'field list'

我正在使用 Spring Boot 版本 2.2.5.RELEASE 和 org.springframework.boot:spring-boot-starter-data-jdbc 对应于 spring-data-jdbc 版本1.1.5.RELEASE

<小时/>

解决方案1:将 ID 字段添加到类中

正如 @chrylis-onstrike- 建议的那样,我在所有数据类中添加了 @Id 字段

class Game(
    @MappedCollection(idColumn = "game")
    var rules: List<Rule> = emptyList(),
){
    @Id
    var id: Long? = null
}

data class Rule(
    @MappedCollection(idColumn = "rule", keyColumn = "rule_key")
    val ruleValues: List<RuleValue>
) {
    @Id
    var id: Long? = null <---- this is new
}

data class RuleValue(val value: String) {
    @Id
    var id: Long? = null  <---- this is new
}

架构更改为

create table game
(
    id              serial primary key
);

create table rule
(
    id              serial primary key,
    game            long references game (id),
    game_key        integer
);

create table rule_value
(
    id               serial primary key,
#    game             long references game (id), <---- removed this
#    game_key         integer,                   <---- removed this
    rule             long references rule (id),
    rule_key         integer,
    value            varchar(256)
);

<小时/>

解决方案2:将复合主键添加到脚本中

class Game(
    @MappedCollection(idColumn = "game")
    var rules: List<Rule> = emptyList(),
){
    @Id
    var id: Long? = null
}

data class Rule(
    @MappedCollection(idColumn = "rule", keyColumn = "rule_key")
    val ruleValues: List<RuleValue>
)

data class RuleValue(val value: String) 

架构更改为

create table game
(
    id              serial primary key
);

create table rule
(
#    id      serial primary key, <---- removed 
    game     integer,
    game_key integer,
    primary key (game, game_key) <---- added 
);

create table rule_value
(
#    id      serial primary key, <---- removed 
    game     integer, <---- added 
    game_key integer, <---- added 
    rule_key integer,
    value    varchar(2048),
    primary key (game, game_key, rule_key) <---- added 
);

<小时/>

结论 您需要确定您的对象是值对象还是实体。值对象在类中可能不应该有 id 字段,因为它的标识是由它携带的值定义的。但是,您需要识别数据库表中的一行并将其与其所有者相关联,因此如果您想将它们保留在不同的表上,则需要在值对象中创建复合主键。

最佳答案

  1. 未找到所需的标识符属性。

    我同意@chrylis-onstrike - 该错误告诉您添加带有@Id注释的字段。 但我也同意你的观点:情况不应该如此。 我至少需要完整的堆栈跟踪才能了解发生了什么。 如果有复制器就更好了。 欢迎在 https://jira.spring.io/browse/DATAJDBC 上创建问题

    对所提供的重现器的进一步调查表明,问题是由 id 列仍然存在于数据库中并且是 identity 列引起的。 因此,在插入后 JDBC 确实返回一个值,并且 Spring Data JDBC 尝试将其设置为实体上的 id,然后由于问题中提到的异常而无法设置该 id。

  2. 为什么我需要 rule_value 表中的 game 和 game_key 列?

    只要 Rule 没有专用 id,其主键就是 gamegame_key 的组合,因此引用来自 rule_value 后面由这两个字段组成。

    Rule 有 ID 时,它们不应该是必需的。

关于java - Spring数据JDBC : Required identifier property not found for a MappedCollection,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60495551/

相关文章:

android-studio - Android Studio 快速文档始终为 "fetching documentation"

多平台环境下Kotlin反射/动态函数调用

java - 在Java中打印数组的内容

java - 如何从特定适配器位置的 RecyclerView 获取 View 并从这些 View 中获取值?

java - 需要从 jvm 中删除 fusionreactor 条目

php - 如何在Wordpress插件前端使用Shortcode显示表单?

mysql 全文索引

java 正则表达式 - 匹配除少数字符串之外的所有字符串

mysql - 如何使用 activerecord 在 JOIN 中指定 'USE INDEX'

Kotlin 集合 : Subtracting two nullable list give incorrect result