Duplicate key value from Hibernate when using a sequence for the primary key
Issue
I somestimes see a containt violation when saving a new entity instance to the database.
INFO [STDOUT] Hibernate: select nextval ('hibernate_sequence')
ERROR [JDBCExceptionReporter] ERROR: duplicate key value violates unique constraint "my_entity_pk"
CREATE TABLE public.my_entity (
id INTEGER NOT NULL DEFAULT nextval('public.my_entity_id_seq'),
...
)
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
@Column(name = "id", unique = true, nullable = false)
@NotNull
public int getId() {
return this.id;
}
Environment
- Hibernate 3.3
Subscriber exclusive content
A Red Hat subscription provides unlimited access to our knowledgebase, tools, and much more.