我在尝试保存实体时遇到错误。我只有在启动服务器时才会收到此错误,而不是在使用 dbunit 运行单元测试时。
我正在尝试保存一个关联。我的单元测试应该与我在手动测试时遇到的情况完全相同。我在之前不存在任何关系的关系的一端添加了一个新实体。
我将 HSQLDB 与单元测试一起使用,而 Web 应用程序正在使用 SQL Server。
我进行的搜索并未证明是富有成效的。对消息的解释将证明非常有用。
这是测试用例(效果很好!):
@Test
@DatabaseSetup(value="MobileWebsiteTest.saveMobilewebsiteMobilecolorswatchmapuserdefined_NewUserSwatch.xml", type=DatabaseOperation.REFRESH)
public void saveMobilewebsiteMobilecolorswatchmapuserdefined_NewUserSwatch() {
// given
Integer mobileWebsiteId = 569;
Mobilecolorswatchmapuserdefined expected = MobilecolorswatchmapuserdefinedBuilder.init().build();
// when
Mobilewebsite result = service.saveMobilewebsiteMobilecolorswatchmapuserdefined(mobileWebsiteId, expected);
// then
assertNotNull("The Mobilewebsite user defined swatch should not be null", result.getMobilecolorswatchmapuserdefined());
assertNotNull("The Mobilewebsite user defined swatch id should not be null.", result.getMobilecolorswatchmapuserdefined().getMobileColorSwatchMapUserDefinedId());
assertEquals("The result aside property should be equivalent to the expected aside property.", expected.getAside(), result.getMobilecolorswatchmapuserdefined().getAside());
assertEquals("The result SiteName property should be equivalent to the expected SiteName property.", expected.getSiteName(), result.getMobilecolorswatchmapuserdefined().getSiteName());
}
这是服务(注意:我目前在已经存在关联的情况下进行删除/保存 - 这应该是一对一的,但是是遗留的):
@Override
@Transactional
public Mobilewebsite saveMobilewebsiteMobilecolorswatchmapuserdefined(Integer mobileWebsiteId, Mobilecolorswatchmapuserdefined related_swatchmap) {
log.debug("saveMobilewebsiteMobilecolorswatchmapuserdefined(Integer mobileWebsiteId=[" + mobileWebsiteId + "], Mobilecolorswatchmapuserdefined related_swatchmap=[" + related_swatchmap + "])");
Mobilewebsite mobilewebsite = mobilewebsiteDAO.findMobilewebsiteByPrimaryKey(mobileWebsiteId, -1, -1);
Calendar now = Calendar.getInstance();
if (mobilewebsite.getMobilecolorswatchmapuserdefined() != null) {
this.deleteMobilewebsiteMobilecolorswatchmapuserdefined(mobilewebsite.getMobileWebsiteId(), mobilewebsite.getMobilecolorswatchmapuserdefined().getMobileColorSwatchMapUserDefinedId());
}
related_swatchmap.setCreatedDtstamp(now);
related_swatchmap.setLastUpdatedDtstamp(now);
related_swatchmap.addToMobilewebsites(mobilewebsite);
**related_swatchmap = mobilecolorswatchmapuserdefinedDAO.store(related_swatchmap);**
mobilewebsite.setMobilecolorswatchmapuserdefined(related_swatchmap);
mobilewebsite = mobilewebsiteDAO.store(mobilewebsite);
mobilewebsiteDAO.flush();
return mobilewebsite;
}
堆栈跟踪指向上面突出显示的行:mobilecolorswatchmapuserdefinedDAO.store(related_swatchmap);
。
堆栈跟踪是:
2012-12-11 15:14:11.192 59464 [http-8080-2] ERROR - Unexpected error occurred
org.apache.wicket.WicketRuntimeException: Method onFormSubmitted of interface org.apache.wicket.markup.html.form.IFormSubmitListener targeted at [ [Component id = form1]] on component [ [Component id = form1]] threw an exception
at org.apache.wicket.RequestListenerInterface.internalInvoke(RequestListenerInterface.java:270)
at org.apache.wicket.RequestListenerInterface.invoke(RequestListenerInterface.java:216)
at org.apache.wicket.request.handler.ListenerInterfaceRequestHandler.invokeListener(ListenerInterfaceRequestHandler.java:248)
at org.apache.wicket.request.handler.ListenerInterfaceRequestHandler.respond(ListenerInterfaceRequestHandler.java:234)
at org.apache.wicket.request.cycle.RequestCycle$HandlerExecutor.respond(RequestCycle.java:781)
at org.apache.wicket.request.RequestHandlerStack.execute(RequestHandlerStack.java:64)
at org.apache.wicket.request.cycle.RequestCycle.execute(RequestCycle.java:255)
at org.apache.wicket.request.cycle.RequestCycle.processRequest(RequestCycle.java:212)
at org.apache.wicket.request.cycle.RequestCycle.processRequestAndDetach(RequestCycle.java:283)
at org.apache.wicket.protocol.http.WicketFilter.processRequest(WicketFilter.java:185)
at org.apache.wicket.protocol.http.WicketFilter.doFilter(WicketFilter.java:241)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:859)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:602)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
at java.lang.Thread.run(Thread.java:662)
Caused by: java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.apache.wicket.RequestListenerInterface.internalInvoke(RequestListenerInterface.java:260)
... 22 more
**Caused by: java.lang.NullPointerException: null entities are not supported by org.hibernate.event.def.EventCache**
at org.hibernate.event.def.EventCache.containsKey(EventCache.java:80)
at org.hibernate.event.def.DefaultMergeEventListener.mergeTransientEntity(DefaultMergeEventListener.java:361)
at org.hibernate.event.def.DefaultMergeEventListener.entityIsTransient(DefaultMergeEventListener.java:303)
at org.hibernate.event.def.DefaultMergeEventListener.onMerge(DefaultMergeEventListener.java:258)
at org.hibernate.event.def.DefaultMergeEventListener.onMerge(DefaultMergeEventListener.java:84)
at org.hibernate.impl.SessionImpl.fireMerge(SessionImpl.java:859)
at org.hibernate.impl.SessionImpl.merge(SessionImpl.java:843)
at org.hibernate.impl.SessionImpl.merge(SessionImpl.java:847)
at org.hibernate.ejb.AbstractEntityManagerImpl.merge(AbstractEntityManagerImpl.java:682)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.springframework.orm.jpa.ExtendedEntityManagerCreator$ExtendedEntityManagerInvocationHandler.invoke(ExtendedEntityManagerCreator.java:365)
at $Proxy44.merge(Unknown Source)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.springframework.orm.jpa.SharedEntityManagerCreator$SharedEntityManagerInvocationHandler.invoke(SharedEntityManagerCreator.java:240)
at $Proxy44.merge(Unknown Source)
at org.skyway.spring.util.dao.AbstractJpaDao.merge(AbstractJpaDao.java:61)
at org.skyway.spring.util.dao.AbstractJpaDao.store(AbstractJpaDao.java:48)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:309)
at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:183)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:110)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:202)
at $Proxy76.store(Unknown Source)
**at com.telventdtn.aghostmobile.service.MobilewebsiteServiceImpl.saveMobilewebsiteMobilecolorswatchmapuserdefined(MobilewebsiteServiceImpl.java:342)**
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:309)
at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:183)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:110)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:202)
at $Proxy77.saveMobilewebsiteMobilecolorswatchmapuserdefined(Unknown Source)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.apache.wicket.proxy.LazyInitProxyFactory$JdkHandler.invoke(LazyInitProxyFactory.java:416)
at org.apache.wicket.proxy.$Proxy4.saveMobilewebsiteMobilecolorswatchmapuserdefined(Unknown Source)
at com.telventdtn.aghostmobile.ArrangeColorsThemePage$SubmitButtonHandler.onSubmit(ArrangeColorsThemePage.java:126)
at com.telventdtn.aghostmobile.markup.html.form.ObservableButton.notifyObservers(ObservableButton.java:20)
at com.telventdtn.aghostmobile.markup.html.form.ButtonFactory$1.onSubmit(ButtonFactory.java:34)
at org.apache.wicket.markup.html.form.Form.delegateSubmit(Form.java:1151)
at org.apache.wicket.markup.html.form.Form.process(Form.java:834)
at org.apache.wicket.markup.html.form.Form.onFormSubmitted(Form.java:762)
at org.apache.wicket.markup.html.form.Form.onFormSubmitted(Form.java:692)
... 27 more