1
存在し、私は次のPostgreSQLのテーブルを持っている:春データPostgreSQLのキーが既に私の春ブーツ/データ/ JPAアプリケーションでエラー2
CREATE TABLE IF NOT EXISTS levels (
id SERIAL PRIMARY KEY,
name VARCHAR(256),
created_date timestamp,
updated_date timestamp
);
と春データ/ JPAエンティティ:
@Entity
@Table(name = "levels")
public class Level extends BaseEntity implements Serializable {
private static final long serialVersionUID = 642499791438799548L;
@Id
@SequenceGenerator(name = "levels_id_seq", sequenceName = "levels_id_seq", allocationSize = 1)
@GeneratedValue(strategy = GenerationType.AUTO, generator = "levels_id_seq")
private Long id;
private String name;
...
}
また、私は、データベースに10のレベルを追加しました:
INSERT INTO levels (id, name) VALUES (1, 'Level 1') ON CONFLICT (id) DO NOTHING;
INSERT INTO levels (id, name) VALUES (2, 'Level 2') ON CONFLICT (id) DO NOTHING;
INSERT INTO levels (id, name) VALUES (3, 'Level 3') ON CONFLICT (id) DO NOTHING;
INSERT INTO levels (id, name) VALUES (4, 'Level 4') ON CONFLICT (id) DO NOTHING;
INSERT INTO levels (id, name) VALUES (5, 'Level 5') ON CONFLICT (id) DO NOTHING;
INSERT INTO levels (id, name) VALUES (6, 'Level 6') ON CONFLICT (id) DO NOTHING;
INSERT INTO levels (id, name) VALUES (7, 'Level 7') ON CONFLICT (id) DO NOTHING;
INSERT INTO levels (id, name) VALUES (8, 'Level 8') ON CONFLICT (id) DO NOTHING;
INSERT INTO levels (id, name) VALUES (9, 'Level 9') ON CONFLICT (id) DO NOTHING;
INSERT INTO levels (id, name) VALUES (10, 'Level 10') ON CONFLICT (id) DO NOTHING;
私は新しいレベルを作成してみてください:
を@Repository
public interface LevelRepository extends JpaRepository<Level, Long> {
Level findByName(String name);
}
level = new Level();
level.setName(name);
level = levelRepository.save(level);
私は次の例外を受信してい
:
システムはid=1
とない例
11
のために選択したのはなぜ
ERROR: duplicate key violates unique constraint « levels_pkey »
DETAIL: Key "(id)=(1)" already exists.
?