我一直在努力解决错误并在SO上阅读了很多帖子,但没有一个解决方案 工作。 我想查询数据库并从表中获取记录列表。
以下是执行此操作的方法 -
public <T> List<T> retrieveByNamedQuery(final String queryName) throws EpPersistenceException {
if (LOG.isDebugEnabled()) {
LOG.debug(LOG_NAMED_QUERY + queryName);
}
try {
return entityManager.createNamedQuery(queryName).getResultList();
} catch (final DataAccessException e) {
throw new EpPersistenceException(CAUGHT_AN_EXCEPTION, e);
}
}
以下是我定义命名查询的xml如何:
<entity class="ProductAssociationImpl">
<named-native-query name="GET_PLAN_PARENT_DEVICE_UIDPK">
<query>SELECT PA.* FROM TPRODUCTASSOCIATION PA, TPRODUCT P WHERE pa.TARGET_PRODUCT_UID = 100163 AND PA.SOURCE_PRODUCT_UID = P.UIDPK AND P.END_DATE IS NULL AND P.PRODUCT_TYPE_UID = 1</query>
</named-native-query>
</entity>
这是xml
中的bean定义<beans>
<!-- Entity Manager Factory -->
<bean id="entityManagerFactory" class="com.elasticpath.persistence.impl.LocalApplicationEntityManagerFactoryBean">
<property name="persistenceUnitName" value="commerce-persistence-unit"/>
<property name="persistenceXmlLocation" value="classpath*:META-INF/persistence-renamed.xml"/>
</bean>
<bean id="sharedEntityManager" class="org.springframework.orm.jpa.support.SharedEntityManagerBean">
<property name="entityManagerFactory">
<ref bean="entityManagerFactory" />
</property>
</bean>
<bean id="sessionFactory" class="com.elasticpath.persistence.impl.JpaSessionFactoryImpl">
<property name="entityManagerFactory" ref="entityManagerFactory"/>
</bean>
<bean id="persistenceEngineTarget"
class="com.elasticpath.persistence.impl.JpaPersistenceEngineImpl">
<property name="entityManager" ref="sharedEntityManager"/>
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<bean id="persistenceEngine"
class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="proxyInterfaces">
<value>com.elasticpath.persistence.PersistenceEngine</value>
</property>
<property name="target">
<ref local="persistenceEngineTarget" />
</property>
</bean>
<bean id="transactionManager"
class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory">
<ref bean="entityManagerFactory"/>
</property>
</bean>
</beans>
当我运行我的代码时,我得到了这个例外:
<openjpa-1.0.1-rexported fatal user error> org.apache.renamed.openjpa.persistence.InvalidStateException: The context has been closed. The stack trace at which the context was closed is held in the embedded exception.
FailedObject: java.lang.IllegalStateException
at org.apache.renamed.openjpa.kernel.BrokerImpl.assertOpen(BrokerImpl.java:4302)
at org.apache.renamed.openjpa.kernel.BrokerImpl.beginOperation(BrokerImpl.java:1710)
at org.apache.renamed.openjpa.kernel.QueryImpl.execute(QueryImpl.java:783)
at org.apache.renamed.openjpa.kernel.QueryImpl.execute(QueryImpl.java:769)
at org.apache.renamed.openjpa.kernel.QueryImpl.execute(QueryImpl.java:765)
at org.apache.renamed.openjpa.kernel.DelegatingQuery.execute(DelegatingQuery.java:517)
at org.apache.renamed.openjpa.persistence.QueryImpl.execute(QueryImpl.java:238)
at org.apache.renamed.openjpa.persistence.QueryImpl.getResultList(QueryImpl.java:277)
at com.elasticpath.persistence.impl.JpaPersistenceEngineImpl.retrieveByNamedQuery(JpaPersistenceEngineImpl.java:460)
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:592)
at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:307)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:198)
at $Proxy58.retrieveByNamedQuery(Unknown Source)
at com.acn.sfweb.controller.impl.ACNAddToCartControllerImpl.handleRequestInternal(ACNAddToCartControllerImpl.java:300)
at org.springframework.web.servlet.mvc.AbstractController.handleRequest(AbstractController.java:153)
at org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter.handle(SimpleControllerHandlerAdapter.java:48)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:875)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:807)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:571)
at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:511)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:710)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at com.elasticpath.commons.filter.impl.CachingControlFilter.doFilter(CachingControlFilter.java:168)
at com.elasticpath.sfweb.filters.EpFilterToBeanProxy.doFilter(EpFilterToBeanProxy.java:137)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at com.elasticpath.sfweb.filters.CustomerSessionFilter.doFilter(CustomerSessionFilter.java:76)
at com.elasticpath.sfweb.filters.EpFilterToBeanProxy.doFilter(EpFilterToBeanProxy.java:137)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.acegisecurity.util.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:265)
at org.acegisecurity.intercept.web.FilterSecurityInterceptor.invoke(FilterSecurityInterceptor.java:107)
at org.acegisecurity.intercept.web.FilterSecurityInterceptor.doFilter(FilterSecurityInterceptor.java:72)
at org.acegisecurity.util.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:275)
at org.acegisecurity.ui.ExceptionTranslationFilter.doFilter(ExceptionTranslationFilter.java:124)
at org.acegisecurity.util.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:275)
at org.acegisecurity.ui.AbstractProcessingFilter.doFilter(AbstractProcessingFilter.java:271)
at org.acegisecurity.util.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:275)
at org.acegisecurity.ui.logout.LogoutFilter.doFilter(LogoutFilter.java:110)
at com.elasticpath.commons.filter.impl.LogoutFilter.doFilter(LogoutFilter.java:49)
at org.acegisecurity.util.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:275)
at org.acegisecurity.ui.logout.LogoutFilter.doFilter(LogoutFilter.java:110)
at com.elasticpath.commons.filter.impl.LogoutFilter.doFilter(LogoutFilter.java:49)
at org.acegisecurity.util.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:275)
at org.acegisecurity.context.HttpSessionContextIntegrationFilter.doFilter(HttpSessionContextIntegrationFilter.java:249)
at org.acegisecurity.util.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:275)
at org.acegisecurity.securechannel.ChannelProcessingFilter.doFilter(ChannelProcessingFilter.java:138)
at org.acegisecurity.util.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:275)
at org.acegisecurity.util.FilterChainProxy.doFilter(FilterChainProxy.java:149)
at org.acegisecurity.util.FilterToBeanProxy.doFilter(FilterToBeanProxy.java:98)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at com.elasticpath.commons.filter.impl.EncodingFilter.handleFilter(EncodingFilter.java:138)
at com.elasticpath.commons.filter.impl.EncodingFilter.doFilter(EncodingFilter.java:81)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:96)
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:230)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
at org.jboss.web.tomcat.security.SecurityAssociationValve.invoke(SecurityAssociationValve.java:179)
at org.jboss.web.tomcat.security.JaccContextValve.invoke(JaccContextValve.java:84)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:104)
at org.jboss.web.tomcat.service.jca.CachedConnectionValve.invoke(CachedConnectionValve.java:157)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:241)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:844)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:580)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)
at java.lang.Thread.run(Thread.java:595)
在很多SO帖子和其他博客中,人们提到应用程序xml中可能缺少tx-manager,但我可以看到它已经存在(见上文)。 这是我在bean定义中的片段
<bean id="transactionManager"
class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory">
<ref bean="entityManagerFactory"/>
</property>
</bean>
P.S。 - 这是一段我无法升级的旧代码,使用的是Spring 2.0 Xml而不是注释
我错过了什么?