Duplicate key value from Hibernate when using a sequence for the primary key

Solution Verified - Updated -

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.

Current Customers and Partners

Log in for full access

Log In

New to Red Hat?

Learn more about Red Hat subscriptions

Using a Red Hat product through a public cloud?

How to access this content