2015-11-09 2 views
1

Мне нужно вызвать хранимую процедуру в SQL Server с помощью Spring JPARepository, я выполнил именно то, что предлагается в документации, но я получаю сообщение об ошибке «Недопустимое сочетание позиционных и именованных параметров» Ниже I дали все классы и хранимые процедуры, которые я использовал, когда я делаю вызов filterCreatives (...) в JPARepository, он терпит неудачу с указанной ошибкой , и, как вы знаете, если я делаю возвращаемый тип filterCreatives (..) void он проходит сейчас. Как я могу заставить код работать с возвращаемым типом, так как List I видел ту же проблему в переполнении стека в течение длительного времени, но он не отвечаетвызов хранимой процедуры весеннее загрузочное приложение с использованием jparepository

Пожалуйста, помогите мне, если есть решение из коробки судимый поиск в сети стоял тщетно nks заранее.

хранимых процедур в SQL Server:

Alter Procedure filtercreatives 
    /* Input Parameters */ 
    @category_id nvarchar(MAX), 
    @advertiser_id nvarchar(MAX), 
    @platform_id nvarchar(MAX), 
    @size_id nvarchar(MAX), 
    @template_id nvarchar(MAX), 
    @language_id nvarchar(MAX) 

AS 
    BEGIN 
    /* Variable Declaration */ 
    Declare @SQLQuery AS NVarchar(4000) 
    Declare @ParamDefinition AS NVarchar (2000) 
    /* Build the Transact-SQL String with the input parameters */ 
    Set @SQLQuery = 'Select * From creativegallery_config where ' 
    /* check for the condition AND build the WHERE clause accordingly */ 

       If (@category_id is not null AND @category_id != '') 
     Set @SQLQuery = @SQLQuery + ' category_id in ('[email protected]_id+') AND' 

    If (@advertiser_id is not null AND @advertiser_id != '') 
     Set @SQLQuery = @SQLQuery + ' advertiser_id in ('[email protected]_id+') AND' 

    If (@platform_id is not null AND @platform_id != '') 
     Set @SQLQuery = @SQLQuery + ' platform_id in ('[email protected]_id+') AND' 

    If (@size_id is not null AND @size_id != '') 
     Set @SQLQuery = @SQLQuery + ' size_id in ('[email protected]_id+') AND' 

       If (@template_id is not null AND @template_id != '') 
     Set @SQLQuery = @SQLQuery + ' template_id in ('[email protected]_id+') AND' 

       If (@language_id is not null AND @language_id != '') 
     Set @SQLQuery = @SQLQuery + ' language_id in ('[email protected]_id+') ' 
       Set @SQLQuery = RTRIM(@SQLQuery) 

       Declare @len as varchar(200) 
       Set @len = LEN(@SQLQuery) 
       if(CHARINDEX('DNA',REVERSE(@SQLQuery)) < 2 and CHARINDEX('DNA',REVERSE(@SQLQuery)) >0) 
           Set @SQLQuery = SUBSTRING(@SQLQuery,1,LEN(@SQLQuery)-3) 
       if(CHARINDEX('EREHW',REVERSE(@SQLQuery)) < 2 and CHARINDEX('EREHW',REVERSE(@SQLQuery)) >0) 
           Set @SQLQuery = SUBSTRING(@SQLQuery,1,LEN(@SQLQuery)-5) 

        Execute sp_Executesql  @SQLQuery 


    If @@ERROR <> 0 GoTo ErrorHandler 
    Return(0) 

ErrorHandler: 
    Return(@@ERROR) 
END 

Java объект класса:

@NamedStoredProcedureQuery(
      name="filtercreatives", 
      procedureName="filtercreatives", 
      parameters={ 
        @StoredProcedureParameter( mode=ParameterMode.IN, type = String.class,name="@category_id"), 
        @StoredProcedureParameter( mode=ParameterMode.IN, type = String.class,name="@advertiser_id"), 
        @StoredProcedureParameter( mode=ParameterMode.IN, type = String.class,name="@platform_id"), 
        @StoredProcedureParameter( mode=ParameterMode.IN, type = String.class,name="@size_id"), 
        @StoredProcedureParameter( mode=ParameterMode.IN, type = String.class,name="@template_id"), 
        @StoredProcedureParameter( mode=ParameterMode.IN, type = String.class,name="@language_id") 
      },resultClasses=CreativeConfig.class 
     ) 
@Entity 
@Table(name = "creativegallery_config") 
public class CreativeConfig implements Serializable{ 
     private static final long serialVersionUID = 6201074363091569476L; 
     @Id 
     @GeneratedValue(strategy = GenerationType.IDENTITY) 
     private Integer id; 
     private Integer advertiserId; 
     private Integer sizeId; 
     private Integer languageId; 
     private Integer platformId; 
     private Integer templateId; 
     private Integer categoryId; 
     private String configUrl; 
     private String tileImageUrl; 


} 

JPA Repository Интерфейс:

@Repository 
public interface CreativeConfigRepository extends JpaRepository<CreativeConfig, Integer> { 

     List<CreativeConfig> findByAdvertiserIdIn(List<Integer> advertiserId); 
     @Procedure(name = "filtercreatives") 
     List<CreativeConfig> filterCreatives(@Param("@category_id")String categoryId,@Param("@advertiser_id") String advertiserId,@Param("@platform_id") String platformId,@Param("@size_id") String sizeId, 
        @Param("@template_id")String templateId, @Param("@language_id")String languageId 
        ); 
} 

Трассировка стека:

org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.springframework.dao.InvalidDataAccessApiUsageException: Invalid mix of named and positional parameters; nested exception is java.lang.IllegalArgumentException: Invalid mix of named and positional parameters 
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:979) 
    at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:858) 
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:687) 
    at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:843) 
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:790) 
    at org.eclipse.jetty.servlet.ServletHolder.handle(ServletHolder.java:808) 
    at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1669) 
    at org.eclipse.jetty.websocket.server.WebSocketUpgradeFilter.doFilter(WebSocketUpgradeFilter.java:224) 
    at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1652) 
    at org.springframework.boot.actuate.autoconfigure.EndpointWebMvcAutoConfiguration$ApplicationContextHeaderFilter.doFilterInternal(EndpointWebMvcAutoConfiguration.java:295) 
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) 
    at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1652) 
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:330) 
    at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.invoke(FilterSecurityInterceptor.java:118) 
    at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.doFilter(FilterSecurityInterceptor.java:84) 
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342) 
    at org.springframework.security.web.access.ExceptionTranslationFilter.doFilter(ExceptionTranslationFilter.java:113) 
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342) 
    at org.springframework.security.web.session.SessionManagementFilter.doFilter(SessionManagementFilter.java:103) 
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342) 
    at org.springframework.security.web.authentication.AnonymousAuthenticationFilter.doFilter(AnonymousAuthenticationFilter.java:113) 
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342) 
    at org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter.doFilter(SecurityContextHolderAwareRequestFilter.java:154) 
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342) 
    at org.springframework.security.web.savedrequest.RequestCacheAwareFilter.doFilter(RequestCacheAwareFilter.java:45) 
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342) 
    at org.springframework.security.web.authentication.www.BasicAuthenticationFilter.doFilter(BasicAuthenticationFilter.java:150) 
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342) 
    at org.springframework.security.web.authentication.logout.LogoutFilter.doFilter(LogoutFilter.java:110) 
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342) 
    at com.ignitionone.config.WebSecurityConfig$1.doFilterInternal(WebSecurityConfig.java:76) 
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) 
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342) 
    at org.springframework.security.web.csrf.CsrfFilter.doFilterInternal(CsrfFilter.java:85) 
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) 
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342) 
    at org.springframework.security.web.header.HeaderWriterFilter.doFilterInternal(HeaderWriterFilter.java:57) 
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) 
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342) 
    at org.springframework.security.web.context.SecurityContextPersistenceFilter.doFilter(SecurityContextPersistenceFilter.java:87) 
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342) 
    at org.springframework.security.web.context.request.async.WebAsyncManagerIntegrationFilter.doFilterInternal(WebAsyncManagerIntegrationFilter.java:50) 
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) 
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342) 
    at org.springframework.security.web.FilterChainProxy.doFilterInternal(FilterChainProxy.java:192) 
    at org.springframework.security.web.FilterChainProxy.doFilter(FilterChainProxy.java:160) 
    at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1652) 
    at org.springframework.boot.actuate.trace.WebRequestTraceFilter.doFilterInternal(WebRequestTraceFilter.java:102) 
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) 
    at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1652) 
    at org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:77) 
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) 
    at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1652) 
    at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:85) 
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) 
    at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1652) 
    at org.springframework.boot.actuate.autoconfigure.MetricsFilter.doFilterInternal(MetricsFilter.java:69) 
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) 
    at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1652) 
    at org.eclipse.jetty.servlet.ServletHandler.doHandle(ServletHandler.java:585) 
    at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:143) 
    at org.eclipse.jetty.security.SecurityHandler.handle(SecurityHandler.java:577) 
    at org.eclipse.jetty.server.session.SessionHandler.doHandle(SessionHandler.java:223) 
    at org.eclipse.jetty.server.handler.ContextHandler.doHandle(ContextHandler.java:1127) 
    at org.eclipse.jetty.servlet.ServletHandler.doScope(ServletHandler.java:515) 
    at org.eclipse.jetty.server.session.SessionHandler.doScope(SessionHandler.java:185) 
    at org.eclipse.jetty.server.handler.ContextHandler.doScope(ContextHandler.java:1061) 
    at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:141) 
    at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:97) 
    at org.eclipse.jetty.server.Server.handle(Server.java:499) 
    at org.eclipse.jetty.server.HttpChannel.handle(HttpChannel.java:310) 
    at org.eclipse.jetty.server.HttpConnection.onFillable(HttpConnection.java:257) 
    at org.eclipse.jetty.io.AbstractConnection$2.run(AbstractConnection.java:540) 
    at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:635) 
    at org.eclipse.jetty.util.thread.QueuedThreadPool$3.run(QueuedThreadPool.java:555) 
    at java.lang.Thread.run(Thread.java:745) 
Caused by: org.springframework.dao.InvalidDataAccessApiUsageException: Invalid mix of named and positional parameters; nested exception is java.lang.IllegalArgumentException: Invalid mix of named and positional parameters 
    at org.springframework.orm.jpa.EntityManagerFactoryUtils.convertJpaAccessExceptionIfPossible(EntityManagerFactoryUtils.java:381) 
    at org.springframework.orm.jpa.vendor.HibernateJpaDialect.translateExceptionIfPossible(HibernateJpaDialect.java:223) 
    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.$Proxy91.FilterCreatives(Unknown Source) 
    at com.ignitionone.service.ConfigAdvertiserDBService.getConfigs(ConfigAdvertiserDBService.java:176) 
    at com.ignitionone.service.ConfigAdvertiserDBService.getConfigs(ConfigAdvertiserDBService.java:154) 
    at com.ignitionone.controller.CreativesController.getCreativess(CreativesController.java:46) 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
    at java.lang.reflect.Method.invoke(Method.java:497) 
    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:110) 
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:776) 
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:705) 
    at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85) 
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:959) 
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:893) 
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:967) 
    ... 75 common frames omitted 
Caused by: java.lang.IllegalArgumentException: Invalid mix of named and positional parameters 
    at org.hibernate.jpa.internal.StoredProcedureQueryImpl.getOutputParameterValue(StoredProcedureQueryImpl.java:279) 
    at org.springframework.data.jpa.repository.query.StoredProcedureJpaQuery.extractOutputValue(StoredProcedureJpaQuery.java:120) 
    at org.springframework.data.jpa.repository.query.JpaQueryExecution$ProcedureExecution.doExecute(JpaQueryExecution.java:298) 
    at org.springframework.data.jpa.repository.query.JpaQueryExecution.execute(JpaQueryExecution.java:74) 
    at org.springframework.data.jpa.repository.query.AbstractJpaQuery.doExecute(AbstractJpaQuery.java:99) 
    at org.springframework.data.jpa.repository.query.AbstractJpaQuery.execute(AbstractJpaQuery.java:90) 
    at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.doInvoke(RepositoryFactorySupport.java:415) 
    at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.invoke(RepositoryFactorySupport.java:393) 
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) 
    at org.springframework.data.repository.core.support.RepositoryFactorySupport$DefaultMethodInvokingMethodInterceptor.invoke(RepositoryFactorySupport.java:506) 
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) 
    at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:99) 
    at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:281) 
    at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:96) 
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) 
    at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:136) 
    ... 98 common frames omitted 
Caused by: org.hibernate.procedure.ParameterStrategyException: Attempt to access positional parameter [7] but ProcedureCall using named parameters 
    at org.hibernate.procedure.internal.ProcedureCallImpl.getParameterRegistration(ProcedureCallImpl.java:329) 
    at org.hibernate.procedure.internal.ProcedureOutputsImpl.getOutputParameterValue(ProcedureOutputsImpl.java:68) 
    at org.hibernate.jpa.internal.StoredProcedureQueryImpl.getOutputParameterValue(StoredProcedureQueryImpl.java:276) 
    ... 113 common frames omitted 
+0

Просьба поделиться трассировкой стека с ошибкой – Makoton

+0

@Makoton Основная причина проблемы заключается в том, что хранимая процедура должна отправлять данные, которые могут быть отправлены в List , тогда он найдет выходной параметр (т.е. позицию 7), я был не понимая, как настроить результаты выполненного запроса в хранимой процедуре для отображения в List , который является возвращаемым значением интерфейса JPARepository. –

+0

Чтобы избежать сочетания позиционных и именованных параметров, вы можете использовать атрибут 'outputParameterName'' @ Procedure'. По умолчанию используется '' '', который возвращается к позиционному, используя 'parameters.size() + 1' в выполнении. Вы пробовали это? –

ответ

1

Я нашел ответ на этот вопрос в другом запросе. Я не могу найти его сейчас. Но по существу, вы должны удалить @NamedStoredProcedureQuery из вашего класса объекта и вызова процедуры непосредственно в классе обслуживания:

@PersistenceContext 
private EntityManager em; 

// your method structure here 

StoredProcedureQuery query = em.CreateStoredProcedureQuery("filtercreatives", CreativeConfig.class); 
query.registerStoredProcedureParameter(0, String.class, ParameterMode.IN); 
/* 
* repeat the above format for each parameter. Notice the use of 
* digits instead of parameter names. Then..... 
*/ 
query.setParameter(0,"your value"); 

В I примечание стороны, другой «Гоча» Я обнаружил, что возвращаемые параметры были даны подчёркивания , даже если их не было, если бы использовался верблюд. Итак, если у вас есть проблемы с этим, не обнаруживая «configUrl», и ваша база данных не имеет «config_url», попробуйте изменить ее в своем коде на «configurl».

Смежные вопросы