Java Spring数据JDBC:找不到MappedCollection所需的标识符属性
简而言之,我有一个Java Spring数据JDBC:找不到MappedCollection所需的标识符属性,java,mysql,kotlin,spring-data,spring-data-jdbc,Java,Mysql,Kotlin,Spring Data,Spring Data Jdbc,简而言之,我有一个游戏作为根聚合。一个游戏有一个规则的列表(顺序很重要),规则有一个规则值的列表(顺序很重要)。规则值目前只是一个字符串,但将来可能会扩展 我有两个问题: 1) 找不到必需的标识符属性:当我尝试保存游戏时,我得到的原因是:java.lang.IllegalStateException:找不到类com的必需标识符属性…RuleValue! 异常消息 2) java.sql.SQLSyntaxErrorException:为什么我需要规则值表中的游戏和游戏密钥列?如果我不放它们,我会
游戏
作为根聚合。一个游戏
有一个规则的列表(顺序很重要)
,规则
有一个规则值的列表(顺序很重要)
。规则值目前只是一个字符串,但将来可能会扩展
我有两个问题:
1) 找不到必需的标识符属性:当我尝试保存游戏时,我得到的原因是:java.lang.IllegalStateException:找不到类com的必需标识符属性…RuleValue!
异常消息
2) java.sql.SQLSyntaxErrorException:为什么我需要规则值
表中的游戏
和游戏密钥
列?如果我不放它们,我会得到由以下原因引起的:java.sql.SQLSyntaxErrorException:字段列表中的未知列“game\u key”
我正在使用SpringBootVersion2.2.5.RELEASE和org.springframework.Boot:SpringBootStarterDataJDBC
对应于SpringDataJDBC
Version1.1.5.RELEASE
解决方案1:将ID字段添加到类中
正如@chrylis onstrike-建议的那样,我在所有数据类中添加了@Id
字段
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)
);
类游戏(
@MappedCollection(idColumn=“游戏”)
变量规则:List=emptyList(),
){
@身份证
变量id:Long?=null
}
数据类规则(
@MappedCollection(idColumn=“rule”,keyColumn=“rule\u key”)
val规则值:列表
) {
@身份证
变量id:Long?=null
找不到必需的标识符属性
我同意@chrylis onstrike-错误告诉您添加带有@Id
注释的字段。
但我也同意你的看法:情况不应该如此。
我至少需要一个完整的堆栈跟踪来了解发生了什么。
复制机会更好。
请随意在上创建一个问题
对所提供的复制机进行的进一步调查表明,问题是由于数据库中仍然存在id
列,并且该列是identity
列造成的。
因此,在insert和Spring数据被JDBC设置为实体上的id之后,JDBC会返回一个值,但由于问题中提到的异常,无法设置该id
为什么我需要规则值
表中的game和game\u key
列
只要规则
没有专用id,它的主键就是游戏
和游戏
的组合,因此规则_值
后面的引用包含这两个字段
当规则
具有id时,它们不应该是必需的
好吗?您的RuleValue
类似乎只有一个属性String value
@chrylis onstrike-规则值目前只是一个字符串,但将来可能会扩展。
问题不在于它只是一个字符串值。这正是问题所在:您的类没有主键,而这正是Spr所做的ing Data正在告诉你。@chrylis onstrike-在所有教程中,此类数据类没有Id
,但是聚合根目录为什么我需要向数据类添加和Id
?你的问题现在很长,包含一个历史、一个解决方案和另一个问题。它看起来更像是一个讨论。关于2-游戏密钥和游戏是不必要的ry,我猜你把它和规则弄混了。@hevi你是对的,我误解了这个问题。更新了我的答案。Id
问题恐怕是由Kotlin spring数据jdbc互操作性问题引起的。我担心这可能是正确的。你能用复制程序创建问题吗?用从复制程序收集的信息更新了答案公爵。
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)
);
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)
);
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
);