org.hibernate.PersistentObjectException:传递给createQuery的select语句中的分离实体

时间:2017-09-03 07:23:38

标签: java hibernate jpa exception

组和规则具有一对多的双向关系。当我在entityManager上调用getSingleResult方法时,我得到了PersistentObjectException:传递给persist的分离实体:Rule

查询是:

entityManager.createQuery("select t from Template t join t.batchContacts bc 
join t.groups g where t.enabled = 1 and t.eventType.id=:eventTypeId and 
t.media=:media and bc.code =:contactCode and "
+ "g.id =:groupId and t.codes is null");

堆栈跟踪:

org.hibernate.PersistentObjectException: detached entity passed to persist: com.tosan.chapar.core.entity.Rule
    at org.hibernate.event.def.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:127) ~[hibernate-core-3.6.0.Final.jar:3.6.0.Final]
    at org.hibernate.impl.SessionImpl.firePersistOnFlush(SessionImpl.java:834) ~[hibernate-core-3.6.0.Final.jar:3.6.0.Final]
    at org.hibernate.impl.SessionImpl.persistOnFlush(SessionImpl.java:826) ~[hibernate-core-3.6.0.Final.jar:3.6.0.Final]
    at org.hibernate.engine.CascadingAction$9.cascade(CascadingAction.java:357) ~[hibernate-core-3.6.0.Final.jar:3.6.0.Final]
    at org.hibernate.engine.Cascade.cascadeToOne(Cascade.java:392) ~[hibernate-core-3.6.0.Final.jar:3.6.0.Final]
    at org.hibernate.engine.Cascade.cascadeAssociation(Cascade.java:335) ~[hibernate-core-3.6.0.Final.jar:3.6.0.Final]
    at org.hibernate.engine.Cascade.cascadeProperty(Cascade.java:204) ~[hibernate-core-3.6.0.Final.jar:3.6.0.Final]
    at org.hibernate.engine.Cascade.cascadeCollectionElements(Cascade.java:425) ~[hibernate-core-3.6.0.Final.jar:3.6.0.Final]
    at org.hibernate.engine.Cascade.cascadeCollection(Cascade.java:362) ~[hibernate-core-3.6.0.Final.jar:3.6.0.Final]
    at org.hibernate.engine.Cascade.cascadeAssociation(Cascade.java:338) ~[hibernate-core-3.6.0.Final.jar:3.6.0.Final]
    at org.hibernate.engine.Cascade.cascadeProperty(Cascade.java:204) ~[hibernate-core-3.6.0.Final.jar:3.6.0.Final]
    at org.hibernate.engine.Cascade.cascade(Cascade.java:161) ~[hibernate-core-3.6.0.Final.jar:3.6.0.Final]
    at org.hibernate.event.def.AbstractFlushingEventListener.cascadeOnFlush(AbstractFlushingEventListener.java:154) ~[hibernate-core-3.6.0.Final.jar:3.6.0.Final]
    at org.hibernate.event.def.AbstractFlushingEventListener.prepareEntityFlushes(AbstractFlushingEventListener.java:145) ~[hibernate-core-3.6.0.Final.jar:3.6.0.Final]
    at org.hibernate.event.def.AbstractFlushingEventListener.flushEverythingToExecutions(AbstractFlushingEventListener.java:88) ~[hibernate-core-3.6.0.Final.jar:3.6.0.Final]
Wrapped by: javax.persistence.PersistenceException: org.hibernate.PersistentObjectException: detached entity passed to persist: com.tosan.chapar.core.entity.Rule
    at org.hibernate.ejb.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1214) ~[hibernate-entitymanager-3.6.0.Final.jar:3.6.0.Final]
    at org.hibernate.ejb.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1147) ~[hibernate-entitymanager-3.6.0.Final.jar:3.6.0.Final]
    at org.hibernate.ejb.QueryImpl.getSingleResult(QueryImpl.java:307) ~[hibernate-entitymanager-3.6.0.Final.jar:3.6.0.Final]
    at com.tosan.chapar.core.persistence.jpa.TemplateDaoJpa.getTemplate(TemplateDaoJpa.java:262) ~[classes/:na]
    at com.tosan.chapar.core.service.impl.TemplateServiceImpl.findEnableGroupAndBatchTemplate(TemplateServiceImpl.java:280) ~[classes/:na]
    at com.tosan.chapar.core.service.impl.TemplateServiceImpl$$FastClassBySpringCGLIB$$2a5066e2.invoke(<generated>) ~[classes/:na]
    at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204) ~[spring-core-4.2.9.RELEASE.jar:4.2.9.RELEASE]
    at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:720) ~[spring-aop-4.2.9.RELEASE.jar:4.2.9.RELEASE]
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157) ~[spring-aop-4.2.9.RELEASE.jar:4.2.9.RELEASE]
    at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:99) ~[spring-tx-4.2.9.RELEASE.jar:4.2.9.RELEASE]
    at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:281) ~[spring-tx-4.2.9.RELEASE.jar:4.2.9.RELEASE]
    at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:96) ~[spring-tx-4.2.9.RELEASE.jar:4.2.9.RELEASE]
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) ~[spring-aop-4.2.9.RELEASE.jar:4.2.9.RELEASE]
    at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:92) ~[spring-aop-4.2.9.RELEASE.jar:4.2.9.RELEASE]
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) ~[spring-aop-4.2.9.RELEASE.jar:4.2.9.RELEASE]

我读了类似的问题并尝试调试。但是我仍然无法找到为什么我会得到这个例外以及如何解决它。我需要一些东西来为我揭开神秘面纱,为什么我得到了它,以及如何以最小的副作用解决,因为这些实体在我的项目中使用不同的地方而我没有时间测试所有场景。

0 个答案:

没有答案