开发者

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

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:

开发者_Python百科
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;
    }


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.


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.


I had a problem with a similar manifestation to yours. I eventually discovered that the configuration of my database connection was wrong: I was connecting to an old database that had an incorrect schema. The new schema declared the primary-key column as

"ID" BIGINT NOT NULL GENERATED ALWAYS AS IDENTITY (START WITH 1, INCREMENT BY 1)

so the database itself automatically generated the primary key whereas the old schema declared it as

"ID" INTEGER NOT NULL

Hibernate executed the correct code for the new schema, which failed on the old schema because the old schema demanded the SQL INSERT provide a value for the ID column.


Or try with @GeneratedValue(strategy = GenerationType.AUTO) instead of @GeneratedValue(strategy = GenerationType.SEQUENCE).


Hibernate fails in silent and mysterious ways when the ID column is an Int. Try changing it to Long in the code and an unsigned 64-bit integer in the database. That fixed the issue for me.


Just in case someone arrives at this thread with the same problem as me. I have been struggling with this for some hours now, and just want to post my solution. It seemed the ID was always set to 0, and that threw an error.

The id property in the model must be of type "Long" or "Integer" and not "long" or "integer" (notice the capitalization). I had put "long", which is a primitive type and cannot be null. Therefore the default value was 0 and Hibernate didn't realize that it was a new entity and tried to save it with ID = 0, something that obviously did not work.

Changing this into the reference type "Long", which can be null, solved it.


I was struggling with the same problem and somehow for me long work instead of LONG type :). As a workarround I converted id column in database as SERIEL. I am using postgres db.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜