我有一个JPA实体类RequestSummary,它最初是从数据库视图中填充的。我正在尝试重构此操作以出于性能原因使用存储过程,但是在设置正确时遇到问题。
我有一个存储过程'sp_bsc_request_summary',它将staffId作为参数。
我的spring数据存储库看起来像这样
public interface RequestSummaryRepository extends JpaRepository<RequestSummary, Long> {
@Procedure("procedureFindAll")
public List<RequestSummary> procedureFindAll(@Param("staffId") Long staffId);
}
我的JPA实体看起来像这样......
@Entity
@Table(name = "vw_bsc_request_summary")
@NamedStoredProcedureQuery(
name = "procedureFindAll",
procedureName = "sp_bsc_request_summary",
resultClasses = {RequestSummary.class},
parameters = {@StoredProcedureParameter(mode = ParameterMode.IN, type = Long.class, name = "staffId")})
public class RequestSummary {
@Id
@Column(name = "bsc_request_id")
private Long id;
....etc
所以有一些我不确定的事情
使用上面的代码,当我尝试运行集成测试来调用方法
时@Test
public void testFindAll() {
final List<RequestSummary> requests = this.summaryRepo.procedureFindAll(61953104L);
Assert.assertNotNull(requests);
Assert.assertTrue(requests.size() > 0);
}
然后我得到一个抱怨null HibernateType的堆栈跟踪
org.springframework.dao.InvalidDataAccessApiUsageException: Type cannot be null; nested exception is java.lang.IllegalArgumentException: Type cannot be null
at org.springframework.orm.jpa.EntityManagerFactoryUtils.convertJpaAccessExceptionIfPossible(EntityManagerFactoryUtils.java:381)
at org.springframework.orm.jpa.vendor.HibernateJpaDialect.translateExceptionIfPossible(HibernateJpaDialect.java:157)
at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.translateExceptionIfPossible(AbstractEntityManagerFactoryBean.java:417)
at org.springframework.dao.support.ChainedPersistenceExceptionTranslator.translateExceptionIfPossible(ChainedPersistenceExceptionTranslator.java:59)
at org.springframework.dao.support.DataAccessUtils.translateIfNecessary(DataAccessUtils.java:213)
at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:147)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.data.jpa.repository.support.CrudMethodMetadataPostProcessor$CrudMethodMetadataPopulatingMethodIntercceptor.invoke(CrudMethodMetadataPostProcessor.java:122)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:92)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:207)
at com.sun.proxy.$Proxy54.procedureFindAll(Unknown Source)
at com.hsbc.gbgcf.bsc.repository.RequestRepositoryIntegrationTest.testFindAll(RequestRepositoryIntegrationTest.java:111)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:74)
at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:83)
at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:72)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:231)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:88)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:71)
at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:174)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
Caused by: java.lang.IllegalArgumentException: Type cannot be null
at org.hibernate.procedure.internal.AbstractParameterRegistrationImpl.setHibernateType(AbstractParameterRegistrationImpl.java:182)
at org.hibernate.procedure.internal.AbstractParameterRegistrationImpl.<init>(AbstractParameterRegistrationImpl.java:131)
at org.hibernate.procedure.internal.AbstractParameterRegistrationImpl.<init>(AbstractParameterRegistrationImpl.java:140)
at org.hibernate.procedure.internal.AbstractParameterRegistrationImpl.<init>(AbstractParameterRegistrationImpl.java:97)
at org.hibernate.procedure.internal.NamedParameterRegistration.<init>(NamedParameterRegistration.java:41)
at org.hibernate.procedure.internal.ProcedureCallImpl.registerParameter(ProcedureCallImpl.java:344)
at org.hibernate.jpa.internal.StoredProcedureQueryImpl.registerStoredProcedureParameter(StoredProcedureQueryImpl.java:152)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.springframework.orm.jpa.SharedEntityManagerCreator$DeferredQueryInvocationHandler.invoke(SharedEntityManagerCreator.java:362)
at com.sun.proxy.$Proxy68.registerStoredProcedureParameter(Unknown Source)
at org.springframework.data.jpa.repository.query.StoredProcedureJpaQuery.newAdhocStoredProcedureQuery(StoredProcedureJpaQuery.java:175)
at org.springframework.data.jpa.repository.query.StoredProcedureJpaQuery.createStoredProcedure(StoredProcedureJpaQuery.java:130)
at org.springframework.data.jpa.repository.query.StoredProcedureJpaQuery.doCreateQuery(StoredProcedureJpaQuery.java:89)
at org.springframework.data.jpa.repository.query.StoredProcedureJpaQuery.createQuery(StoredProcedureJpaQuery.java:80)
at org.springframework.data.jpa.repository.query.JpaQueryExecution$ProcedureExecution.doExecute(JpaQueryExecution.java:298)
at org.springframework.data.jpa.repository.query.JpaQueryExecution.execute(JpaQueryExecution.java:77)
at org.springframework.data.jpa.repository.query.AbstractJpaQuery.doExecute(AbstractJpaQuery.java:100)
at org.springframework.data.jpa.repository.query.AbstractJpaQuery.execute(AbstractJpaQuery.java:91)
at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.doInvoke(RepositoryFactorySupport.java:393)
at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.invoke(RepositoryFactorySupport.java:371)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:98)
at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:262)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:95)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:136)
... 37 more
我看了很多其他帖子和Spring.io文档,但我仍然在努力解决这个问题。
有什么想法吗?
更新
所以根据 lanemax 的建议......我的服务方法如下:
public List<RequestSummary> findAll(final Long staffId) {
final StoredProcedureQuery storedProcedureQuery = this.entityManager.createNamedStoredProcedureQuery("procedureFindAll");
storedProcedureQuery.setParameter("staffId", staffId);
storedProcedureQuery.execute();
return storedProcedureQuery.getResultList();
}
和我的实体类一样
@NamedStoredProcedureQuery(name = "procedureFindAll", procedureName = "sp_bsc_request_summary", parameters = {
@StoredProcedureParameter(mode = ParameterMode.IN, type = Long.class, name = "staffId")}, resultSetMappings = "mapping")
@SqlResultSetMapping(name = "mapping", classes = {@ConstructorResult(targetClass = RequestSummary.class, columns = {
@ColumnResult(name = "bsc_request_id", type = Long.class), @ColumnResult(name = "created_date", type = Date.class),
@ColumnResult(name = "transaction_size", type = BigDecimal.class),
@ColumnResult(name = "approval_status_id", type = Long.class),
@ColumnResult(name = "approval_status_desc", type = String.class), @ColumnResult(name = "approval_key", type = String.class),
@ColumnResult(name = "captain_staff_id", type = Long.class), @ColumnResult(name = "captain_name", type = String.class),
@ColumnResult(name = "captain_country_id", type = Long.class), @ColumnResult(name = "captain_country", type = String.class),
@ColumnResult(name = "est_fee_usd", type = BigDecimal.class), @ColumnResult(name = "client_vision_id", type = String.class),
@ColumnResult(name = "client_full_name", type = String.class), @ColumnResult(name = "client_country_id", type = Long.class),
@ColumnResult(name = "country_description", type = String.class), @ColumnResult(name = "mastergroup", type = String.class),
@ColumnResult(name = "decision_staff_id", type = Long.class), @ColumnResult(name = "decision_staff_name", type = String.class),
@ColumnResult(name = "temporary_client_name", type = String.class)
})})
@Entity
public class RequestSummary {
@Id
private Long id;
当我调用方法时,虽然我得到了如下的堆栈跟踪
2016-12-16 10:58:10,565 ERROR [http-bio-8080-exec-28] controller.RequestController - Current CallableStatement ou was not a ResultSet, but getResultList was called
java.lang.IllegalStateException: Current CallableStatement ou was not a ResultSet, but getResultList was called
at org.hibernate.jpa.internal.StoredProcedureQueryImpl.getResultList(StoredProcedureQueryImpl.java:336)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.springframework.orm.jpa.SharedEntityManagerCreator$DeferredQueryInvocationHandler.invoke(SharedEntityManagerCreator.java:362)
at com.sun.proxy.$Proxy82.getResultList(Unknown Source)
at com.company.gbgcf.bsc.service.RequestService.findAll(RequestService.java:112)
at com.company.gbgcf.bsc.service.RequestService.findAll(RequestService.java:125)
at com.company.gbgcf.bsc.controller.RequestController.findAllRequests(RequestController.java:102)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:221)
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:137)
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:104)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:747)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:676)
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:938)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:870)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:961)
at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:852)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:837)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:369)
at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.invoke(FilterSecurityInterceptor.java:109)
at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.doFilter(FilterSecurityInterceptor.java:83)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:381)
at org.springframework.security.web.access.ExceptionTranslationFilter.doFilter(ExceptionTranslationFilter.java:97)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:381)
at org.springframework.security.web.session.SessionManagementFilter.doFilter(SessionManagementFilter.java:100)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:381)
at org.springframework.security.web.authentication.AnonymousAuthenticationFilter.doFilter(AnonymousAuthenticationFilter.java:78)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:381)
at org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter.doFilter(SecurityContextHolderAwareRequestFilter.java:54)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:381)
at org.springframework.security.web.savedrequest.RequestCacheAwareFilter.doFilter(RequestCacheAwareFilter.java:35)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:381)
at org.springframework.security.web.authentication.www.BasicAuthenticationFilter.doFilter(BasicAuthenticationFilter.java:177)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:381)
at org.springframework.security.web.authentication.ui.DefaultLoginPageGeneratingFilter.doFilter(DefaultLoginPageGeneratingFilter.java:91)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:381)
at org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter.doFilter(AbstractAuthenticationProcessingFilter.java:187)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:381)
at com.company.gbgcf.bsc.filters.AuthenticatedSessionFilter.doFilter(AuthenticatedSessionFilter.java:143)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:381)
at org.springframework.security.web.authentication.logout.LogoutFilter.doFilter(LogoutFilter.java:105)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:381)
at org.springframework.security.web.context.SecurityContextPersistenceFilter.doFilter(SecurityContextPersistenceFilter.java:79)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:381)
at org.springframework.security.web.FilterChainProxy.doFilter(FilterChainProxy.java:168)
at org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:344)
at org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:261)
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:171)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:947)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1009)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:312)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at java.lang.Thread.run(Thread.java:724)
最初我认为这可能是因为程序打印出一些调试信息以及记录集,但是删除了这些打印语句我仍然得到同样的错误......
更新2
添加更多日志记录确认调用了正确的过程并且参数已绑定...
2016-12-16 11:54:53,883 DEBUG [http-bio-8080-exec-25] spi.SqlStatementLogger -
{call sp_bsc_request_summary(?)}
Hibernate:
{call sp_bsc_request_summary(?)}
2016-12-16 11:54:53,904 TRACE [http-bio-8080-exec-25] sql.BasicBinder - binding parameter [1] as [BIGINT] - [61953104]
2016-12-16 11:54:53,984 DEBUG [http-bio-8080-exec-1] annotation.AbstractMessageConverterMethodProcessor - Written [{"forename":"CRAIG","surname":"GORDON","roleNames":["DP_DEFAULT_VIEW_LAF","DP_MENU_DELETE","GB_ADVISORY_DEAL_STATUS_UPDATE_REPORT_USER","DP_ADD_NEW_DEAL","DP_CF_Report","GB_CAPITAL_FINANCING_REPORT","LAF_MD_PIPELINE_REPORT_USER","LAF_CANNED_REPORT","DCM_DEAL_PIPELINE_USER","DP_DEAL_PIPELINE_USER","GB_ADVISORY_NEW_DEALS_REPORT_USER","LAF_DEAL_CLOSE_REPORT","DP_SHOW_CLIENT_ON_REPORTS","DCM_CAPITAL_FINANCING_REPORT","LAF_DEAL_PIPELINE_USER","GB_CANNED_REPORT","DP_EXT_Deal_Access_DCM","GB_DEAL_PIPELINE_USER","Clone_GB_Deal","DP_EXT_Deal_Access_MnA"]}] as "application/json;charset=UTF-8" using [org.springframework.http.converter.StringHttpMessageConverter@d3d0bbc]
2016-12-16 11:54:53,986 DEBUG [http-bio-8080-exec-1] servlet.DispatcherServlet - Null ModelAndView returned to DispatcherServlet with name 'bsc': assuming HandlerAdapter completed request handling
2016-12-16 11:54:53,986 DEBUG [http-bio-8080-exec-1] servlet.FrameworkServlet - Successfully completed request
2016-12-16 11:54:53,988 DEBUG [http-bio-8080-exec-1] access.ExceptionTranslationFilter - Chain processed normally
2016-12-16 11:54:53,988 DEBUG [http-bio-8080-exec-1] context.SecurityContextPersistenceFilter - SecurityContextHolder now cleared, as request processing completed
Building Return [isResultSet=false, updateCount=0, extendedReturn=false
2016-12-16 11:54:54,597 DEBUG [http-bio-8080-exec-25] jpa.EntityManagerFactoryUtils - Closing JPA EntityManager
2016-12-16 11:54:54,617 ERROR [http-bio-8080-exec-25] controller.RequestController - Current CallableStatement ou was not a ResultSet, but getResultList was called
java.lang.IllegalStateException: Current CallableStatement ou was not a ResultSet, but getResultList was called
at org.hibernate.jpa.internal.StoredProcedureQueryImpl.getResultList(StoredProcedureQueryImpl.java:336)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
更新3
终于搞定了。所以对于其他遇到问题的人....
错误 - Current CallableStatement ou was not a ResultSet
通过在我的存储过程中包含set nocount on
来解决(这是在sybase 15中)
这给我留下了另一个错误,即@ConstructorResult无法找到正确的构造函数。这是因为从过程返回的字段中推断出的某些数据类型与构造函数所期望的数据类型不同。确定下载hibernate源jar并在
ConstructorResultColumnProcessor.resolveConstructor(Class targetClass, List<Type> types)
一旦类型对齐,它就会按预期工作。
答案 0 :(得分:3)
几个月前我们碰到了这个问题,不幸的是,找不到它的解决方案。我们所做的,你可能也是,也就是创建一个服务,注入实体管理器,并从那里调用它。这不是最优的,但是和你一样,我们努力争取获得纯粹的spring-data-jpa解决方案。如果您想沿着这条路走下去,这里有一个适合您的解决方案。
将您的实体定义稍微更改为
@NamedStoredProcedureQuery(name = "procedureFindAll",
procedureName = "sp_bsc_request_summary",
parameters = {
@StoredProcedureParameter(mode = ParameterMode.IN, type = Long.class, name = "staffId")},
,resultSetMappings = "mapping"))
@SqlResultSetMapping(name = "mapping", classes = {
@ConstructorResult(targetClass = RequestSummary.class, columns = {
@ColumnResult(name = "bsc_request_id", type = Long.class)
})
})
在上面,为要映射的每个字段添加ColumnResult。您的服务将如下所示
...
@Autowired
private EntityManager entityManager;
public List<RequestSummary> findAll(Long staffId) {
StoredProcedureQuery storedProcedureQuery = entityManager.createNamedStoredProcedureQuery("sp_bsc_request_summary");
storedProcedureQuery.setParameter("staffId", staffId);
storedProcedureQuery.execute();
return storedProcedureQuery.getResultList();
}
答案 1 :(得分:1)
之前我遇到过这个问题。首先@Procedure
不起作用。所以我选择了@NamedStoredProcedureQuery
。这是有效的,但我发现很多管道或锅炉板代码。我优雅/简洁的解决方案是使用nativeQuery
。试图模仿上面的代码,我的解决方案将如下所示:
public interface ProcedureFindAll extends JpaRepository<RequestSummary, Long> {
@Query(value = "EXECUTE sp_bsc_request_summary :staffId", nativeQuery = true)
List<RequestSummary> findAllRequestSummary( @Param( "staffId" ) Long staffId);
}
您的实体将只有实体注释。
@Entity
public class RequestSummary {
@Id
private Long id;
//...code remove for brevity
}
答案 2 :(得分:0)
来自here的解决方案为我工作:更改
public List<RequestSummary> findAll(final Long staffId) {...
到
public Object[] findAll(final Long staffId) {...