sequence jpa - @GeneratedValue(strategy = GenerationType.AUTO) not working as thought

5 Answers

In my case it was about bad dialect:


instead of:


when I switched to the production database. Hibernate tried to use strategy prepared for different db engine.

hibernate_sequence found string

I'm trying to persist an object to a database. Keep getting 'Column ID cannot accept null value error'. My object looks like this:

public class TestTable {
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Integer id = 0;

    @Column(nullable=false, length=256)
    private String data = "";

    public Integer getId() {
        return id;

    public void setId(Integer id) { = id;

    public String getData() {
        return data;

    public void setData(String data) { = data;


My persist function:

public static synchronized boolean persistObject(Object obj){
        boolean success = true;
        EntityManager em = null;
        EntityTransaction tx = null;
            em = getEmf().createEntityManager();
            tx = em.getTransaction();

        } catch (Exception e){
            success = false;
        } finally{
            } catch(Exception e){
        return success;

DB2 Auto generated Column / GENERATED ALWAYS pros and cons over sequence

I tend to use identity columns more than sequences, but I'll compare the two for you.

Sequences can generate numbers for any purpose, while an identity column is strictly attached to a column in a table.

Since a sequence is an independent object, it can generate numbers for multiple tables (or anything else), and is not affected when any table is dropped. When a table with a identity column is dropped, there is no memory of what value was last assigned by that identity column.

A table can have only one identity column, so if you want to want to record multiple sequential numbers into different columns in the same table, sequence objects can handle that.

The most common requirement for a sequential number generator in a database is to assign a technical key to a row, which is handled well by an identity column. For more complicated number generation needs, a sequence object offers more flexibility.

Heres something I found on the publib site:

Comparing IDENTITY columns and sequences

While there are similarities between IDENTITY columns and sequences, there are also differences. The characteristics of each can be used when designing your database and applications.

An identity column has the following characteristics:

  • An identity column can be defined as part of a table only when the table is created. Once a table is created, you cannot alter it to add an identity column. (However, existing identity column characteristics might be altered.)
  • An identity column automatically generates values for a single table.
  • When an identity column is defined as GENERATED ALWAYS, the values used are always generated by the database manager. Applications are not allowed to provide their own values during the modification of the contents of the table.

A sequence object has the following characteristics:

  • A sequence object is a database object that is not tied to any one table.
  • A sequence object generates sequential values that can be used in any SQL or XQuery statement.
  • Since a sequence object can be used by any application, there are two expressions used to control the retrieval of the next value in the specified sequence and the value generated previous to the statement being executed. The PREVIOUS VALUE expression returns the most recently generated value for the specified sequence for a previous statement within the current session. The NEXT VALUE expression returns the next value for the specified sequence. The use of these expressions allows the same value to be used across several SQL and XQuery statements within several tables.

While these are not all of the characteristics of these two items, these characteristics will assist you in determining which to use depending on your database design and the applications using the database.

try it. @GeneratedValue(strategy = GenerationType.IDENTITY) instead of GenerationType.Auto

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.