hibernate - @GeneratedValue(strategy = GenerationType.AUTO) not working as thought -
i'm trying persist object database. keep getting 'column id cannot accept null value error'. object looks 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; }
you may use generationtype.table. way, jpa uses sequence table id assigment , may never need generate sequence or auto-increment values or triggers lowers portability.
also note in java int type initiated 0 default, may rid of also.
Comments
Post a Comment