当前位置: 代码迷 >> Java Web开发 >> org.hibernate.PersistentObjectException: detached entity passed to persist:该如何处理
  详细解决方案

org.hibernate.PersistentObjectException: detached entity passed to persist:该如何处理

热度:533   发布时间:2016-04-17 00:35:31.0
org.hibernate.PersistentObjectException: detached entity passed to persist:
Action 代码:
public String add()throws Exception{
Indentify indentify = new Indentify();
System.out.println(bean1.getIndentifyNum());
indentify.setIndentifyNum(bean1.getIndentifyNum());
indentify.setIndentifyName(bean1.getIndentifyName());
if(indentify != null){
indentifyService.save(indentify);
message = "添加成功";
}else{
return "indentify_add";
}
return "action";
}
save方法用的是:
public Boolean save(T baseBean) {
getHibernateTemplate().persist(baseBean);
return true;
}
为什么会报这种错误?
org.springframework.dao.InvalidDataAccessApiUsageException: detached entity passed to persist: com.xuefeng.entity.Indentify; nested exception is org.hibernate.PersistentObjectException: detached entity passed to persist: com.xuefeng.entity.Indentify
org.springframework.orm.hibernate3.SessionFactoryUtils.convertHibernateAccessException(SessionFactoryUtils.java:651)
org.springframework.orm.hibernate3.HibernateAccessor.convertHibernateAccessException(HibernateAccessor.java:412)
org.springframework.orm.hibernate3.HibernateTemplate.doExecute(HibernateTemplate.java:411)
org.springframework.orm.hibernate3.HibernateTemplate.executeWithNativeSession(HibernateTemplate.java:374)
org.springframework.orm.hibernate3.HibernateTemplate.persist(HibernateTemplate.java:793)
com.xuefeng.service.dao.ToolDao.save(ToolDao.java:34)
com.xuefeng.service.dao.ToolDao$$FastClassByCGLIB$$99083c8a.invoke(<generated>)
net.sf.cglib.proxy.MethodProxy.invoke(MethodProxy.java:149)
org.springframework.aop.framework.Cglib2AopProxy$CglibMethodInvocation.invokeJoinpoint(Cglib2AopProxy.java:692)
org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150)
org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:108)
org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:89)
org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
org.springframework.aop.framework.Cglib2AopProxy$DynamicAdvisedInterceptor.intercept(Cglib2AopProxy.java:625)
com.xuefeng.service.impl.IndentifyServiceImpl$$EnhancerByCGLIB$$f1bdd3b3.save(<generated>)
com.xuefeng.action.admin.IndentifyAdminAction.add(IndentifyAdminAction.java:134)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)


------解决方案--------------------
detached entity passed to persist:是不是自动生成的Id,给手动赋值了?
------解决方案--------------------
Indentify这个类是怎么写的..
在Model 的 setid()中判断id是否为 ""(空字符串,可能由页面传过来为空) 
public void setId(String id) { 
if(!StringUtils.isNotEmpty(id)) 

id=null; 

this.id = id; 
}
  相关解决方案