我在两个实体之间设置了一对多关系-运动会话(父级)和运动(孩子),一个具有很多运动的运动。
但是,当我尝试保存“锻炼”会话时,出现错误消息:
错误:重复的键 值违反唯一约束“ exercise_pkey”详细信息:键 (id)=(21)已经存在。
我不明白为什么练习没有在此处生成唯一键?
@Entity
@Table(name="workout")
public class Workout {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private Long id;
private String name;
private String category;
private String type;
private Timestamp duration;
private String notes;
@OneToMany(cascade = CascadeType.ALL, orphanRemoval = true)
@JoinColumn(name = "id")
private List<Exercise> exercises;
}
@Entity
@Table(name="exercise")
public class Exercise {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private Long id;
@Column(name="name")
private String exerciseName;
}
表创建脚本:
CREATE TABLE WORKOUT(
ID serial PRIMARY KEY NOT NULL,
NAME CHAR(50) NOT NULL,
CATEGORY CHAR(50),
TYPE CHAR(50),
DURATION TIMESTAMP,
NOTES CHAR(500)
);
CREATE TABLE exercise (
id serial primary key not null,
workout integer references workout(id),
name char(100) NOT NULL
);