@GeneratedValue (strategy = GenerationType.AUTO) работает не так, как предполагалось

Я пытаюсь сохранить объект в базе данных. Продолжайте получать «ID столбца не может принимать ошибку нулевого значения». Мой объект выглядит следующим образом:

    @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 функция:

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;
    }
19
задан skaffman 28 February 2011 в 18:00
поделиться