通过与两个现有实体的关系来保存新实体

时间:2014-03-09 20:44:31

标签: java spring hibernate jsp

在使用hibernate的spring mvc应用程序中,当我尝试保存与两个其他实体类型的现有对象相关联的一个实体类型的新对象时,我遇到错误。我已经阅读了许多关于给定错误消息的其他帖子,但这些帖子似乎都没有解决我的具体问题。

我正在尝试保存类型为Encounter的新对象,该对象与特定的现有Provider对象和特定的现有FacilityAddress对象相关联。

这是JSP的控制器方法开始调用自己的方法时给出的错误消息:

org.hibernate.PersistentObjectException: detached entity passed to persist:  
org.springframework.samples.knowledgemanager.model.Provider

以下是处理用户从JSP发送的操作的controller方法的代码:

@RequestMapping(value = "/patients/{patientId}/encounters/new", method = RequestMethod.GET)
public String initCreationForm(@PathVariable("patientId") int patientId, @RequestParam("providerId") int prid, org.springframework.web.context.request.WebRequest webRequest, Map<String, Object> model) {
    Encounter encounter = new Encounter();

    Map<Integer, String> praddrs = new LinkedHashMap<Integer, String>(); 
    for(FacilityAddress adr: this.clinicService.findFacilityAddressByProviderId(prid)){ 
        praddrs.put(adr.getId(), adr.getAddress());
    }

    Provider pr = clinicService.findProviderById(prid);
    model.put("encounter", encounter);
    model.put("praddrs", praddrs);
    model.put("pr", pr);
    return "encounters/createOrUpdateEncounterForm";
}

//THIS IS THE METHOD THAT HANDLES THE FORM
@RequestMapping(value = "/patients/{patientId}/encounters/new", method = {RequestMethod.POST, RequestMethod.PUT})
public String processCreationForm(@ModelAttribute("encounter") Encounter encounter, @RequestParam("providerId") int providerId, @PathVariable("patientId") int patientId, org.springframework.web.context.request.WebRequest webRequest, BindingResult result, SessionStatus status) {
    System.out.println("inside processCreationForm() ");
    Patient patient = this.clinicService.findPatientById(patientId);
    LocalDate theday = new LocalDate(webRequest.getParameter("day"));
    LocalTime thetime = new LocalTime(webRequest.getParameter("time"));
    DateTime thedatetime = theday.toDateTime(thetime);
    encounter.setDateTime(thedatetime);
    patient.addEncounter(encounter);

    System.out.println("encounter.getDateTime() is: "+encounter.getDateTime());
    new EncounterValidator().validate(encounter, result);
    Provider myprovider = this.clinicService.findProviderById(providerId);
    encounter.addProvider(myprovider);
    myprovider.addEncounter(encounter);

    System.out.println(" jjjjjjjjjjjjjjj id, DateTime, getLocationSelected(), getNumMins(), Patient(), Status(), Providers().size() are: "+encounter.getId()+", "+encounter.getDateTime()+", "+encounter.getLocationSelected()+", "+encounter.getNumMins()+", "+encounter.getPatient().getFirstName()+", "+encounter.getStatus()+", "+encounter.getProviders().size());
    encounter.setLocation(this.clinicService.findFacilityAddressById(encounter.getLocationSelected()));
    System.out.println("done validating encounter for errors");
    if (result.hasErrors()) {
        System.out.println("about to return errors. ");
        return "encounters/createOrUpdateEncounterForm";
    } else {
        System.out.println("about to save encounter ");
        this.clinicService.saveEncounter(encounter);
        System.out.println("done saving encounter.");
        status.setComplete();
        System.out.println("finished status.setComplete()");
        return "redirect:/encounters?encounterID={encounterId}";
    }
}

以下是EncounterRepository中最终由上述代码调用的代码,请参阅

@Override
public void save(Encounter encounter) {
    if (encounter.getId() == null) {
        System.out.println("about to persist encounter.");
        this.em.persist(encounter);
        System.out.println("done persisting encounter");
    }
    else {
        System.out.println("about to merge encounter.");
        this.em.merge(encounter);
        System.out.println("done merging encounter.");
    }
}  

为了简化此帖子,我已将实体的代码上传到文件共享网站,您可以通过点击以下链接访问该网站:

可以找到Encounter实体的代码by clicking on this link 可以找到Provider实体的代码by clicking on this link 可以找到FacilityAddress实体的代码by clicking on this link

这是堆栈跟踪:

org.springframework.dao.InvalidDataAccessApiUsageException: detached entity passed to persist: org.springframework.samples.knowledgemanager.model.Provider; nested exception is org.hibernate.PersistentObjectException: detached entity passed to persist: org.springframework.samples.knowledgemanager.model.Provider
    at org.springframework.orm.hibernate3.SessionFactoryUtils.convertHibernateAccessException(SessionFactoryUtils.java:668) ~[spring-orm-3.2.5.RELEASE.jar:3.2.5.RELEASE]
    at org.springframework.orm.jpa.vendor.HibernateJpaDialect.translateExceptionIfPossible(HibernateJpaDialect.java:106) ~[spring-orm-3.2.5.RELEASE.jar:3.2.5.RELEASE]
    at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.translateExceptionIfPossible(AbstractEntityManagerFactoryBean.java:403) ~[spring-orm-3.2.5.RELEASE.jar:3.2.5.RELEASE]
    at org.springframework.dao.support.ChainedPersistenceExceptionTranslator.translateExceptionIfPossible(ChainedPersistenceExceptionTranslator.java:58) ~[spring-tx-3.2.5.RELEASE.jar:3.2.5.RELEASE]
    at org.springframework.dao.support.DataAccessUtils.translateIfNecessary(DataAccessUtils.java:213) ~[spring-tx-3.2.5.RELEASE.jar:3.2.5.RELEASE]
    at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:163) ~[spring-tx-3.2.5.RELEASE.jar:3.2.5.RELEASE]
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172) ~[spring-aop-3.2.5.RELEASE.jar:3.2.5.RELEASE]
    at org.springframework.aop.aspectj.MethodInvocationProceedingJoinPoint.proceed(MethodInvocationProceedingJoinPoint.java:80) ~[spring-aop-3.2.5.RELEASE.jar:3.2.5.RELEASE]
    at org.springframework.samples.knowledgemanager.util.CallMonitoringAspect.invoke(CallMonitoringAspect.java:80) ~[CallMonitoringAspect.class:na]
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.6.0_29]
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) ~[na:1.6.0_29]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) ~[na:1.6.0_29]
    at java.lang.reflect.Method.invoke(Method.java:597) ~[na:1.6.0_29]
    at org.springframework.aop.aspectj.AbstractAspectJAdvice.invokeAdviceMethodWithGivenArgs(AbstractAspectJAdvice.java:621) ~[spring-aop-3.2.5.RELEASE.jar:3.2.5.RELEASE]
    at org.springframework.aop.aspectj.AbstractAspectJAdvice.invokeAdviceMethod(AbstractAspectJAdvice.java:610) ~[spring-aop-3.2.5.RELEASE.jar:3.2.5.RELEASE]
    at org.springframework.aop.aspectj.AspectJAroundAdvice.invoke(AspectJAroundAdvice.java:65) ~[spring-aop-3.2.5.RELEASE.jar:3.2.5.RELEASE]
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172) ~[spring-aop-3.2.5.RELEASE.jar:3.2.5.RELEASE]
    at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:91) ~[spring-aop-3.2.5.RELEASE.jar:3.2.5.RELEASE]
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172) ~[spring-aop-3.2.5.RELEASE.jar:3.2.5.RELEASE]
    at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:204) ~[spring-aop-3.2.5.RELEASE.jar:3.2.5.RELEASE]
    at $Proxy45.save(Unknown Source) ~[na:na]
    at org.springframework.samples.knowledgemanager.service.ClinicServiceImpl.saveEncounter(ClinicServiceImpl.java:204) ~[ClinicServiceImpl.class:na]
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.6.0_29]
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) ~[na:1.6.0_29]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) ~[na:1.6.0_29]
    at java.lang.reflect.Method.invoke(Method.java:597) ~[na:1.6.0_29]
    at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:317) ~[spring-aop-3.2.5.RELEASE.jar:3.2.5.RELEASE]
    at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:183) ~[spring-aop-3.2.5.RELEASE.jar:3.2.5.RELEASE]
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150) ~[spring-aop-3.2.5.RELEASE.jar:3.2.5.RELEASE]
    at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:96) ~[spring-tx-3.2.5.RELEASE.jar:3.2.5.RELEASE]
    at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:260) ~[spring-tx-3.2.5.RELEASE.jar:3.2.5.RELEASE]
    at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:94) ~[spring-tx-3.2.5.RELEASE.jar:3.2.5.RELEASE]
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172) ~[spring-aop-3.2.5.RELEASE.jar:3.2.5.RELEASE]
    at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:204) ~[spring-aop-3.2.5.RELEASE.jar:3.2.5.RELEASE]
    at $Proxy48.saveEncounter(Unknown Source) ~[na:na]
    at org.springframework.samples.knowledgemanager.web.EncounterController.processCreationForm(EncounterController.java:115) ~[EncounterController.class:na]
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.6.0_29]
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) ~[na:1.6.0_29]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) ~[na:1.6.0_29]
    at java.lang.reflect.Method.invoke(Method.java:597) ~[na:1.6.0_29]
    at org.springframework.web.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:219) ~[spring-web-3.2.5.RELEASE.jar:3.2.5.RELEASE]
    at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:132) ~[spring-web-3.2.5.RELEASE.jar:3.2.5.RELEASE]
    at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:104) ~[spring-webmvc-3.2.5.RELEASE.jar:3.2.5.RELEASE]
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:745) ~[spring-webmvc-3.2.5.RELEASE.jar:3.2.5.RELEASE]
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:686) ~[spring-webmvc-3.2.5.RELEASE.jar:3.2.5.RELEASE]
    at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:80) ~[spring-webmvc-3.2.5.RELEASE.jar:3.2.5.RELEASE]
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:925) [spring-webmvc-3.2.5.RELEASE.jar:3.2.5.RELEASE]
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:856) [spring-webmvc-3.2.5.RELEASE.jar:3.2.5.RELEASE]
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:936) [spring-webmvc-3.2.5.RELEASE.jar:3.2.5.RELEASE]
    at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:838) [spring-webmvc-3.2.5.RELEASE.jar:3.2.5.RELEASE]
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:647) [servlet-api.jar:na]
    at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:812) [spring-webmvc-3.2.5.RELEASE.jar:3.2.5.RELEASE]
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:728) [servlet-api.jar:na]
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305) [catalina.jar:7.0.42]
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210) [catalina.jar:7.0.42]
    at org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:77) [spring-web-3.2.5.RELEASE.jar:3.2.5.RELEASE]
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) [spring-web-3.2.5.RELEASE.jar:3.2.5.RELEASE]
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243) [catalina.jar:7.0.42]
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210) [catalina.jar:7.0.42]
    at com.github.dandelion.datatables.core.web.filter.DatatablesFilter.doFilter(DatatablesFilter.java:73) [datatables-core-0.9.2.jar:na]
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243) [catalina.jar:7.0.42]
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210) [catalina.jar:7.0.42]
    at com.github.dandelion.datatables.extras.servlet2.filter.DatatablesFilter.doFilter(DatatablesFilter.java:71) [datatables-servlet2-0.9.2.jar:na]
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243) [catalina.jar:7.0.42]
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210) [catalina.jar:7.0.42]
    at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:88) [spring-web-3.2.5.RELEASE.jar:3.2.5.RELEASE]
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) [spring-web-3.2.5.RELEASE.jar:3.2.5.RELEASE]
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243) [catalina.jar:7.0.42]
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210) [catalina.jar:7.0.42]
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222) [catalina.jar:7.0.42]
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123) [catalina.jar:7.0.42]
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502) [catalina.jar:7.0.42]
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171) [catalina.jar:7.0.42]
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99) [catalina.jar:7.0.42]
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953) [catalina.jar:7.0.42]
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118) [catalina.jar:7.0.42]
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408) [catalina.jar:7.0.42]
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1023) [tomcat-coyote.jar:7.0.42]
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589) [tomcat-coyote.jar:7.0.42]
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:312) [tomcat-coyote.jar:7.0.42]
    at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886) [na:1.6.0_29]
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908) [na:1.6.0_29]
    at java.lang.Thread.run(Thread.java:662) [na:1.6.0_29]
Caused by: org.hibernate.PersistentObjectException: detached entity passed to persist: org.springframework.samples.knowledgemanager.model.Provider
    at org.hibernate.event.internal.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:141) ~[hibernate-core-4.2.1.Final.jar:4.2.1.Final]
    at org.hibernate.internal.SessionImpl.firePersist(SessionImpl.java:843) ~[hibernate-core-4.2.1.Final.jar:4.2.1.Final]
    at org.hibernate.internal.SessionImpl.persist(SessionImpl.java:836) ~[hibernate-core-4.2.1.Final.jar:4.2.1.Final]
    at org.hibernate.ejb.engine.spi.EJB3CascadingAction$1.cascade(EJB3CascadingAction.java:53) ~[hibernate-entitymanager-4.2.1.Final.jar:4.2.1.Final]
    at org.hibernate.engine.internal.Cascade.cascadeToOne(Cascade.java:388) ~[hibernate-core-4.2.1.Final.jar:4.2.1.Final]
    at org.hibernate.engine.internal.Cascade.cascadeAssociation(Cascade.java:331) ~[hibernate-core-4.2.1.Final.jar:4.2.1.Final]
    at org.hibernate.engine.internal.Cascade.cascadeProperty(Cascade.java:209) ~[hibernate-core-4.2.1.Final.jar:4.2.1.Final]
    at org.hibernate.engine.internal.Cascade.cascadeCollectionElements(Cascade.java:418) ~[hibernate-core-4.2.1.Final.jar:4.2.1.Final]
    at org.hibernate.engine.internal.Cascade.cascadeCollection(Cascade.java:358) ~[hibernate-core-4.2.1.Final.jar:4.2.1.Final]
    at org.hibernate.engine.internal.Cascade.cascadeAssociation(Cascade.java:334) ~[hibernate-core-4.2.1.Final.jar:4.2.1.Final]
    at org.hibernate.engine.internal.Cascade.cascadeProperty(Cascade.java:209) ~[hibernate-core-4.2.1.Final.jar:4.2.1.Final]
    at org.hibernate.engine.internal.Cascade.cascade(Cascade.java:166) ~[hibernate-core-4.2.1.Final.jar:4.2.1.Final]
    at org.hibernate.event.internal.AbstractSaveEventListener.cascadeAfterSave(AbstractSaveEventListener.java:449) ~[hibernate-core-4.2.1.Final.jar:4.2.1.Final]
    at org.hibernate.event.internal.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:292) ~[hibernate-core-4.2.1.Final.jar:4.2.1.Final]
    at org.hibernate.event.internal.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:192) ~[hibernate-core-4.2.1.Final.jar:4.2.1.Final]
    at org.hibernate.event.internal.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:125) ~[hibernate-core-4.2.1.Final.jar:4.2.1.Final]
    at org.hibernate.ejb.event.EJB3PersistEventListener.saveWithGeneratedId(EJB3PersistEventListener.java:78) ~[hibernate-entitymanager-4.2.1.Final.jar:4.2.1.Final]
    at org.hibernate.event.internal.DefaultPersistEventListener.entityIsTransient(DefaultPersistEventListener.java:208) ~[hibernate-core-4.2.1.Final.jar:4.2.1.Final]
    at org.hibernate.event.internal.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:151) ~[hibernate-core-4.2.1.Final.jar:4.2.1.Final]
    at org.hibernate.event.internal.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:78) ~[hibernate-core-4.2.1.Final.jar:4.2.1.Final]
    at org.hibernate.internal.SessionImpl.firePersist(SessionImpl.java:853) ~[hibernate-core-4.2.1.Final.jar:4.2.1.Final]
    at org.hibernate.internal.SessionImpl.persist(SessionImpl.java:827) ~[hibernate-core-4.2.1.Final.jar:4.2.1.Final]
    at org.hibernate.internal.SessionImpl.persist(SessionImpl.java:831) ~[hibernate-core-4.2.1.Final.jar:4.2.1.Final]
    at org.hibernate.ejb.AbstractEntityManagerImpl.persist(AbstractEntityManagerImpl.java:875) ~[hibernate-entitymanager-4.2.1.Final.jar:4.2.1.Final]
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.6.0_29]
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) ~[na:1.6.0_29]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) ~[na:1.6.0_29]
    at java.lang.reflect.Method.invoke(Method.java:597) ~[na:1.6.0_29]
    at org.springframework.orm.jpa.SharedEntityManagerCreator$SharedEntityManagerInvocationHandler.invoke(SharedEntityManagerCreator.java:241) ~[spring-orm-3.2.5.RELEASE.jar:3.2.5.RELEASE]
    at $Proxy37.persist(Unknown Source) ~[na:na]
    at org.springframework.samples.knowledgemanager.repository.jpa.JpaEncounterRepositoryImpl.save(JpaEncounterRepositoryImpl.java:101) ~[JpaEncounterRepositoryImpl.class:na]
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.6.0_29]
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) ~[na:1.6.0_29]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) ~[na:1.6.0_29]
    at java.lang.reflect.Method.invoke(Method.java:597) ~[na:1.6.0_29]
    at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:317) ~[spring-aop-3.2.5.RELEASE.jar:3.2.5.RELEASE]
    at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:183) ~[spring-aop-3.2.5.RELEASE.jar:3.2.5.RELEASE]
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150) ~[spring-aop-3.2.5.RELEASE.jar:3.2.5.RELEASE]
    at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:155) ~[spring-tx-3.2.5.RELEASE.jar:3.2.5.RELEASE]
    ... 77 common frames omitted  

2 个答案:

答案 0 :(得分:1)

这种错误通常发生在hibernate延迟初始化时。有两个解决方案,第一个也是最好的是使用OpenSessionInViewFilter进行休眠。另一个是在你的hbm文件中使用急切的初始化。

在过滤器链顶部的项目web.xml中定义OpenSessionInViewFilter。文档为here

答案 1 :(得分:1)

问题是提供程序实例已在单独的事务中加载,并且在传递给save方法时是一个分离的对象。调用em.persist()会因设置CascadeType.ALL而尝试保留提供程序对象,并抛出异常。请注意,所有加载和保存操作都应该在同一个事务中,因此解决方案是将providerIdfacilityAddressId传递给服务(在这种情况下为saveEncounter)并重新加载它们然后设置将这些附加对象添加到encounter,然后保存encounter