javax.persistence.PersistenceException:org.hibernate.PersistentObjectException:分离的实体传递给坚持
Posted
技术标签:
【中文标题】javax.persistence.PersistenceException:org.hibernate.PersistentObjectException:分离的实体传递给坚持【英文标题】:javax.persistence.PersistenceException: org.hibernate.PersistentObjectException: detached entity passed to persist 【发布时间】:2013-02-18 08:54:10 【问题描述】:在 Hibernate 中使用 JPA,运行以下代码时出现异常。第一次运行时,一切正常,数据已插入数据库。第二次,应该更新数据的时候,失败了:
@AdminTx
public void processSite(Site site)
FluxBoutiqueMapping mapping = mapper.generateMappingFromUrl(site);
Boutique boutique;
for (FluxBoutiqueMapping.Boutique fluxBoutique : mapping.getListe().getBoutiques())
log.error("Dans la boucle");
boutique = daoAdmin.namedQuerySingle(Boutique.LOAD_BOUTIQUE_BY_IDWEBSC, fluxBoutique.getId());
log.error("boutique : "+boutique);
if (boutique==null)
log.error("Dans le new");
boutique = new Boutique();
boutique.setSite(site);
boutique.setUrlLogo(fluxBoutique.getLogo());
boutique.setUrlBoutique(fluxBoutique.getUrl());
boutique.setSelected(false);
boutique.setIdWebSC(fluxBoutique.getId());
boutique.setDateModification(new Date());
boutiqueDao.persist(boutique);
boutique = null;
boutiqueDao.persist() 只是调用 EntityManager.persist() 方法。
这里是我的精品课:
@Entity
@Table(name = "BOUTIQUE")
@SequenceGenerator(name = "SEQ_BOUTIQUE", sequenceName = "SEQ_BOUTIQUE")
@NamedQueries(value =
@NamedQuery(name = Boutique.LOAD_BOUTIQUE_BY_IDWEBSC, query = "from Boutique b where b.idWebSC=?1"),
)
public class Boutique implements IPersistentObject, IPubliable<Manifestation>
/**
*
*/
private static final long serialVersionUID = -3038903536445432584L;
public static final String LOAD_BOUTIQUE_BY_IDWEBSC = "load.boutique.by.idwebsc";
protected long idBoutique;
protected Site site;
protected Long idOrigine;
protected String urlLogo;
protected String urlBoutique;
protected boolean selected;
protected long idWebSC;
protected Date datePublication;
protected Date dateModification;
@Override
@Id
@GeneratedValue(generator="SEQ_BOUTIQUE")
@Column(name = "ID_BOUTIQUE", unique = true, nullable = false, precision = 8, scale = 0)
public Long getId()
return this.idBoutique;
public void setId(Long idBoutique)
this.idBoutique = idBoutique;
@Override
public void setIdOrigine(Long idOrigine)
this.idOrigine = idOrigine;
@Override
@Column(name = "IDORIGINE", length = 7)
public Long getIdOrigine()
return this.idOrigine;
@Override
@Temporal(TemporalType.DATE)
@Column(name = "DATE_PUBLICATION", length = 7)
public Date getDatePublication()
return datePublication;
@Override
public void setDatePublication(Date datePublication)
this.datePublication = datePublication;
@Override
@Temporal(TemporalType.DATE)
@Column(name = "DATE_MODIFICATION", length = 7)
public Date getDateModification()
return dateModification;
public void setDateModification(Date dateModification)
this.dateModification = dateModification;
@Override
public void update(Manifestation newer)
// TODO Auto-generated method stub
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "ID_SITE")
@ForeignKey(name = "FK_BOUTIQUE_SITE")
public Site getSite()
return site;
public void setSite(Site site)
this.site = site;
@Column(name = "URL_LOGO", length = 255)
public String getUrlLogo()
return urlLogo;
public void setUrlLogo(String urlLogo)
this.urlLogo = urlLogo;
@Column(name = "URL_BOUTIQUE", length = 255)
public String getUrlBoutique()
return urlBoutique;
public void setUrlBoutique(String urlBoutique)
this.urlBoutique = urlBoutique;
@Column(name = "SELECTED")
public boolean isSelected()
return selected;
public void setSelected(boolean selected)
this.selected = selected;
@Column(name = "ID_WEBSC", length = 7)
public long getIdWebSC()
return idWebSC;
public void setIdWebSC(long idWebSC)
this.idWebSC = idWebSC;
最后,我的堆栈跟踪:
javax.persistence.PersistenceException: org.hibernate.PersistentObjectException: detached entity passed to persist: fr.u2m.viparis.business.Boutique
at org.hibernate.ejb.AbstractEntityManagerImpl.throwPersistenceException(AbstractEntityManagerImpl.java:630)
at org.hibernate.ejb.AbstractEntityManagerImpl.persist(AbstractEntityManagerImpl.java:219)
at fr.u2m.dao.jpa.GenericDaoJPAImpl.persist(GenericDaoJPAImpl.java:60)
at fr.u2m.viparis.service.impl.BoutiqueService.processSite(BoutiqueService.java:81)
at fr.u2m.viparis.service.impl.BoutiqueService.processAllFluxBoutique(BoutiqueService.java:52)
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.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:309)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:196)
at $Proxy136.processAllFluxBoutique(Unknown Source)
at fr.u2m.viparis.fluxboutique.action.FluxBoutiqueAction.loadFlux(FluxBoutiqueAction.java:27)
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.apache.struts.actions.DispatchAction.dispatchMethod(DispatchAction.java:270)
at org.apache.struts.actions.DispatchAction.execute(DispatchAction.java:187)
at fr.u2m.struts.OpenViewRequestProcessor.processJpaActionPerform(OpenViewRequestProcessor.java:270)
at fr.u2m.struts.OpenViewRequestProcessor.processActionPerform(OpenViewRequestProcessor.java:115)
at org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:236)
at fr.u2m.struts.OpenViewRequestProcessor.process(OpenViewRequestProcessor.java:230)
at org.apache.struts.action.ActionServlet.process(ActionServlet.java:1196)
at org.apache.struts.action.ActionServlet.doGet(ActionServlet.java:414)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at fr.u2m.viparis.cms.filter.MultiTabSessionFilter.doFilter(MultiTabSessionFilter.java:75)
at org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:237)
at org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:167)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at fr.u2m.viparis.cms.filter.MonitoringFilter.doFilter(MonitoringFilter.java:54)
at org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:237)
at org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:167)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at fr.u2m.viparis.cms.util.auditing.AuditingFilter.doFilter(AuditingFilter.java:44)
at org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:237)
at org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:167)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at fr.u2m.util.SetCharacterEncodingFilter.doFilter(SetCharacterEncodingFilter.java:71)
at org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:237)
at org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:167)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.springframework.orm.hibernate3.support.OpenSessionInViewFilter.doFilterInternal(OpenSessionInViewFilter.java:198)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:168)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:929)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
at org.apache.coyote.ajp.AjpProcessor.process(AjpProcessor.java:200)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:585)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:310)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
Caused by: org.hibernate.PersistentObjectException: detached entity passed to persist: fr.u2m.viparis.business.Boutique
at org.hibernate.event.def.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:79)
at org.hibernate.event.def.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:38)
at org.hibernate.impl.SessionImpl.firePersist(SessionImpl.java:618)
at org.hibernate.impl.SessionImpl.persist(SessionImpl.java:592)
at org.hibernate.impl.SessionImpl.persist(SessionImpl.java:596)
at org.hibernate.ejb.AbstractEntityManagerImpl.persist(AbstractEntityManagerImpl.java:213)
... 65 more
我在 Tomcat 7.0.32 服务器上运行 Java 7.0.11。
有什么想法吗?
【问题讨论】:
【参考方案1】:Persist
用于全新的瞬态对象,如果 id 已分配,则会失败。您可能应该调用saveOrUpdate
而不是persist
。
或者,您可以检查该对象是否已包含在您的实体管理器中,如果是,则执行
entityManager.merge(yourObject);
,否则
entityManager.persist(yourObject);
【讨论】:
当然... persist() 只是为了插入。我更改了代码,以便为新对象调用persist(),为已经存在的对象调用merge(),它运行良好。谢谢! 不客气。不过,您可以尝试更仔细地查看其他 SO 线程。我发现至少 3 个符合您的问题。 我搜索了,但实际上关于该主题的问题太多了。 要决定是persist()还是merge(),我需要确定实体是否已经存在于实体管理器中。我该怎么做? 通常未保存实体的 id 应为 0 或 -1。所以你可以这样做: if(myEntity.getId()>0)entityManager.persist(myEntity); elseentityManager.merge(myEntity);【参考方案2】:您的主要问题是您在一个 DAO 中加载实体:
boutique = daoAdmin.namedQuerySingle(
Boutique.LOAD_BOUTIQUE_BY_IDWEBSC, fluxBoutique.getId());
然后用另一个保存它们:
boutiqueDao.persist(boutique);
在现有实体的情况下,这将产生一个分离的实体错误,因为该实体有一个 id,但不存在于第二个 DAO 的工作单元中。当然,即使您使用相同的 DAO 进行读取/持久化,您仍然会遇到问题,因为您不应该使用 persist
来保存现有实体。试试这个:
使用与保存实体相同的 DAO 读取实体
精品= BoutiqueDAO.namedQuerySingle( Boutique.LOAD_BOUTIQUE_BY_IDWEBSC,fluxBoutique.getId());
改变你对新精品店的处理方式,做一个立竿见影的对象
if (boutique==null)
log.error("Dans le new");
boutique = new Boutique();
boutiqueDAO.persist(boutique);
将最后一个persist改为merge
boutique.setIdWebSC(fluxBoutique.getId());
boutique.setDateModification(new Date());
boutique = boutiqueDao.merge(boutique);
【讨论】:
以上是关于javax.persistence.PersistenceException:org.hibernate.PersistentObjectException:分离的实体传递给坚持的主要内容,如果未能解决你的问题,请参考以下文章