Today when testing the integration of spring and hibernate, encountered a problem, the error situation is as follows: error message:
Org.springframework.dao.InvalidDataAccessApiUsageException:detached entity passed to Persist:com.bean.Cat; Nested exception is org.hibernate.PersistentObjectException:detached entity passed to Persist:com.bean.Cat at ORG.SP
Ringframework.orm.hibernate3.SessionFactoryUtils.convertHibernateAccessException (sessionfactoryutils.java:668) At Org.springframework.orm.hibernate3.HibernateAccessor.convertHibernateAccessException (Hibernateaccessor.java : 412) at Org.springframework.orm.hibernate3.HibernateTemplate.doExecute (hibernatetemplate.java:411) at ORG.SPRINGF Ramework.orm.hibernate3.HibernateTemplate.executeWithNativeSession (hibernatetemplate.java:374) at Org.springframework.orm.hibernate3.HibernateTemplate.persist (hibernatetemplate.java:793) at Com.dao.impl.CatDaoImpl.createCat (catdaoimpl.java:20) at COM.TEST.TESTCAT.TESTADD (testcat.java:35) at Sun.reflect. NATIVEMETHODACCESSORIMPL.INVOKE0 (Native Method) at Sun.reflect.NativeMethodAccessorImpl.invoke (UnkNown source) at Sun.reflect.DelegatingMethodAccessorImpl.invoke (Unknown source) at Java.lang.reflect.Method.invoke (Unknown Source) at Org.junit.runners.model.frameworkmethod$1.runreflectivecall (frameworkmethod.java:47) at Org.ju Nit.internal.runners.model.ReflectiveCallable.run (Reflectivecallable.java:12) at Org.junit.runners.model.FrameworkMethod.invokeExplosively (frameworkmethod.java:44) at Org.junit.internal.runners.statements.InvokeMethod.evaluate (invokemethod.java:17) at Org.junit.runners.ParentRunner.runLeaf (parentrunner.java:271) at Org.junit.runners.BlockJUnit4ClassRunner.runChild (blockjunit4classrunner.java:70) at Org.junit.runners.BlockJUnit4ClassRunner.runChild (BLOCKJUNIT4CLASSRUNNER.JAVA:50) at Org.junit.runners.parentrunner$3.run (parentrunner.java:238) at Org.junit.runners.parentrunner$1.schedule ( parentrunner.java:63) at Org.junit.runners.ParentRunner.runChildren (parentrunner.java:236) at Org.junit.runners.Pa Rentrunner.access$000 (parentrunner.java:53) at Org.junit.runners.parentrunner$2.evaluate (parentrunner.java:229) at Org.junit.int Ernal.runners.statements.RunBefores.evaluate (runbefores.java:26) at Org.junit.internal.runners.statements.RunAfters.evaluate (runafters.java:27) at Org.junit.runners.ParentRunner.run (parentrunner.java:309) at Org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run (JUNIT4TESTREFERENCE.JAVA:50) at Org.eclipse.jdt.internal.junit.runner.TestExecution.run (testexecution.java:38) at Org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests (remotetestrunner.java:467) at Org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests (remotetestrunner.java:683) at Org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run (remotetestrunner.java:390) at Org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main (remotetestrunner.java:197) caused by: Org.hibernate.PersistentObjectException:detached entity passed to PERSIST:COM.BEAN.CAt-org.hibernate.event.def.DefaultPersistEventListener.onPersist (defaultpersisteventlistener.java:127) at Org.h Ibernate.event.def.DefaultPersistEventListener.onPersist (defaultpersisteventlistener.java:61) at Org.hibernate.impl.SessionImpl.firePersist (sessionimpl.java:808) at Org.hibernate.impl.SessionImpl.persist ( sessionimpl.java:782) at Org.hibernate.impl.SessionImpl.persist (sessionimpl.java:786) at org.springframework.orm.h Ibernate3. Hibernatetemplate$21.doinhibernate (hibernatetemplate.java:796) at Org.springframework.orm.hibernate3.HibernateTemplate.doExecute (hibernatetemplate.java:406) ...
More
cause of error:
From the error message "detached entity passed to Persist:com.bean.Cat", it can be analyzed that the bug was wrong during the Cat persistence conversion. Here is some of the code for the test:
Cat entity class
/**
* @Author: zsh
* @Title: Cat.java
* @Description: Cat entity class
* @Date: September 18, 2016 PM 3:12:23 */
@ Entity
@Table (name = "Cat") public
class Cat {
@Id
@GeneratedValue (strategy = Generationtype.auto) C12/>private Integer ID;
@Column (name = "name")
private String name;
@Temporal (value = temporaltype.timestamp)
private Date createdate;
...... The constructor and setter, getter methods are omitted.
Note: When using annotations, in order to avoid breaking the Java encapsulation feature, it is generally written on the Get method, which is written above the field for ease of analysis.
test Method
@Test public
void Testadd () throws parseexception{
cat cat = new Cat (2, "TOM", New SimpleDateFormat ("Yyyy-mm-dd "). Parse (" 2000-1-1 "));
Cdao.createcat (cat);
}
By analyzing two pieces of code, we can find that the ID field has been set up in the entity class, but when the cat class is instantiated at the time of the test, the value of the ID is specified, so the error is reported at run time. Workaround:
1. Cancel the field auto-increment setting
2. Do not set an ID value when instantiating a Cat object