I have a following PostgreSQL table:
CREATE TABLE IF NOT EXISTS levels (
id SERIAL PRIMARY KEY,
name VARCHAR(256),
created_date timestamp,
updated_date timestamp
);
and corresponding Spring Data entity:
@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.SEQUENCE, generator = "levels_id_seq")
private Integer id;
private String name;
...
}
@MappedSuperclass
public abstract class BaseEntity {
@Column(name = "created_date")
private Date createdDate;
@Column(name = "updated_date")
private Date updatedDate;
@PrePersist
void onCreate() {
setCreatedDate(new Date());
}
@PreUpdate
void onPersist() {
setUpdatedDate(new Date());
}
...
}
this is my Spring Data Level Repository:
@Repository
public interface LevelRepository extends JpaRepository<Level, Integer> {
Level findByName(String name);
}
Right now when I try to save a new Level entity with Spring LevelRepository I receive a following error:
ERROR: duplicate key violates unique constraint « levels_pkey »
DETAIL: Key "(id)=(5)" already exists.
What can be wrong with my configuration ?
strategy=GenerationType.AUTO?