entityManager.persist(); error prone

1. Error message

javax.persistence.PersistenceException: org.hibernate.PersistentObjectException: detached entity passed to persist: com.compass.hibernate.Entity.Customer
Caused by: org.hibernate.PersistentObjectException: detached entity passed to persist: com.compass.hibernate.Entity.Customer
at org.hibernate.event.internal.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:124)
at org.hibernate.event.internal.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:58)
at org.hibernate.internal.SessionImpl.firePersist(SessionImpl.java:775)
at org.hibernate.internal.SessionImpl.persist(SessionImpl.java:748)
at org.hibernate.internal.SessionImpl.persist(SessionImpl.java:753)
at org.hibernate.jpa.spi.AbstractEntityManagerImpl.persist(AbstractEntityManagerImpl.java:1146)
… 66 more

Insert picture description here
The problem is that the id primary key in the entity class is set to auto-increment, but I pass a specific value when passing the value. This is problematic. Even if the id does not exist, an error will be reported.

2. Solution:

Set a null to the id attribute and let it grow by itself, there will be no problem, and the insertion is successful.
After successfully inserting the id to null, I just started this jpa by myself, it is really easy to make this kind of low-level mistakes.
Insert picture description here

Guess you like

Origin blog.csdn.net/m0_46188681/article/details/114372387