Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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

    @Entity
public class TestTable {
    @Id
    @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) {
        this.id = id;
    }

    public String getData() {
        return data;
    }

    public void setData(String data) {
        this.data = data;
    }

}

My persist function:

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

        } catch (Exception e){
            success = false;
        } finally{
            try{
                em.close();
            } catch(Exception e){
                //nothing
            }
        }
        return success;
    }
like image 619
NullPointer0x00 Avatar asked Feb 26 '11 20:02

NullPointer0x00


People also ask

What is the use of @GeneratedValue strategy GenerationType Auto?

GenerationType. AUTO This GenerationType indicates that the persistence provider should automatically pick an appropriate strategy for the particular database. This is the default GenerationType, i.e. if we just use @GeneratedValue annotation then this value of GenerationType will be used.

What is the use of ID GeneratedValue strategy GenerationType identity?

IDENTITY. This GenerationType indicates that the persistence provider must assign primary keys for the entity using a database identity column. IDENTITY column is typically used in SQL Server. This special type column is populated internally by the table itself without using a separate sequence.

How does @GeneratedValue work?

The @GeneratedValue annotation tells the ORM how to figure out the value of that field. Typcial generators you will run into. It is possible to develop custom generator. The interaction with the database will depend on generation strategy.

Can we use @GeneratedValue without @ID?

JPA entity identifier annotations The @Id annotation is mandatory for entities, and it must be mapped to a table column that has a unique constraint. Most often, the @Id annotation is mapped to the Primary Key table column. Without specifying a @GeneratedValue annotation, entity identifiers must be assigned manually.


2 Answers

You may use GenerationType.TABLE. That way, jpa uses a sequence table for id assigment and you may never need to generate sequence or auto-increment values or triggers that lowers portability.

Also note that in java int type is initiated with 0 default, so you may get rid of that also.

like image 147
Cem Güler Avatar answered Sep 22 '22 04:09

Cem Güler


In my case it was about bad dialect:

hibernate.dialect=org.hibernate.dialect.H2Dialect

instead of:

hibernate.dialect=org.hibernate.dialect.PostgreSQL9Dialect

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

like image 23
cyprian Avatar answered Sep 19 '22 04:09

cyprian