Hibernate null constraint violation on @Id with @GeneratedValue Hibernate null constraint violation on @Id with @GeneratedValue postgresql postgresql

Hibernate null constraint violation on @Id with @GeneratedValue


Try the annotation @org.hibernate.annotations.GenericGenerator(name = “test-hilo-strategy”, strategy = “hilo”):

@Id@org.hibernate.annotations.GenericGenerator(name=“hilo-strategy”, strategy = “hilo”)@GeneratedValue(generator = ”hilo-strategy”)

As someone noted above, AUTO does not do what you think. It uses the underlying DB to determine how to generate values. It may pick sequences (for oracle), identity column (for mssql), or something else that is db specific.

The approach here uses an internal strategy that Hibernate supplies called "hilo".

See chapter 5 of the Hibernate reference manual dealing with "Generator" for a full description of what each of the supplied ones does.


Neither the OP solution nor Matt's solution worked with my PostgreSQL 9.3.

But this one works:

@SequenceGenerator(name="identifier", sequenceName="mytable_id_seq", allocationSize=1)  @GeneratedValue(strategy=GenerationType.SEQUENCE, generator="identifier")

Replace mytable_id_seq with the name of the sequence that generates your id.


Use Hibernate method:- save(String entityName, Object object) Persist the given transient instance, first assigning a generated identifier.

Do not use :- @GeneratedValue(strategy=GenerationType.IDENTITY) for primary key if you want to persist user define Id.

For detail:-http://docs.jboss.org/hibernate/core/3.6/javadocs/org/hibernate/Session.html#save(java.lang.String