JPA not insert the data in the row after persist
I am using an ID generated value in my en开发者_如何学Ctity
@Id
@TableGenerator(
name="marcaTable",
table="JPA_WXS_APP_SEQUENCE_GENERATOR",
pkColumnName="GEN_KEY",
valueColumnName="GEN_VALUE",
pkColumnValue="MARCA_ID",
allocationSize=1,
initialValue=0)
@GeneratedValue(strategy=GenerationType.TABLE,generator="marcaTable")
public int getId() {
return Id;
}
I use a table to save the id.
If I execute this code twice its fail because there are duplicates ID (1 id)
public static void main(String[] args) {
EntityManagerFactory emf =
Persistence.createEntityManagerFactory("ACoches");
EntityManager em = emf.createEntityManager();
EntityTransaction tx = em.getTransaction();
tx.begin();
marca nmarca2 = new marca();
nmarca2.setNombre_marca("pepito");
em.flush();
em.persist(nmarca2);
tx.commit();
em.close();
emf.close();
}
}
But if I manually execute a select of marca table it is empty, it seems that JPA dont insert the data in the row just when i make the em.persist(nmarca2);
If I delete the JPA_WXS_APP_SEQUENCE_GENERATOR table manually and I select again the marca table now yes I can see the register.
Thanks in advance!!!
persist() just registers the object to be persisted. It will not be inserted until commit() or flush(). If you call flush() after the persist() it will have been inserted.
Can't see why you would get a duplicate id. Turn logging on finest to see what SQL is being executed.
One issue may be your initialValue=0, try removing or changing it to 1.
精彩评论