2017-10-17 153 views
12

我想用Spring JPA提供的NamedStoredProcedureQuery調用Postgresql中編寫的存儲過程。以下是代碼片段。Spring JPA中的NamedStoredProcedureQuery錯誤 - 「找到與位置參數關聯的命名存儲過程參數」

EntityMovement.java

@Entity 
@Table(name = "entity_movement") 
@NamedStoredProcedureQueries({ 
    @NamedStoredProcedureQuery(name = "near_by_entities", 
           procedureName = "near_by_entities", 
           parameters = { 
            @StoredProcedureParameter(mode = ParameterMode.IN, name = "location", type = String.class), 
            @StoredProcedureParameter(mode = ParameterMode.IN, name = "radius", type = Double.class), 
            @StoredProcedureParameter(mode = ParameterMode.REF_CURSOR, type = void.class) 
           }) 
}) 
public class EntityMovement implements Serializable{ 

//Fields 

//Getters and Setters 

} 

EntityMovementRepository

@Repository 
public interface EntityMovementRepository extends JpaRepository<EntityMovement, Entity>{ 
    @Procedure(name = "near_by_entities") 
    public List<EntityMovement> nearByEntities(@Param("location")String location,@Param("radius")double radius); 

} 

調用

List<EntityMovement> entityMovements= entityMovementRepository.nearByEntities(location, radius); 

Stored Procedure

查詢被簡化

CREATE OR REPLACE FUNCTION public.near_by_entities(
location character varying, 
radius double precision) 
RETURNS refcursor 
LANGUAGE 'plpgsql' 
AS $BODY$ 
DECLARE ref refcursor; 
BEGIN 
OPEN ref FOR SELECT * FROM public.entity_movement; 
RETURN ref; 
END 
$BODY$; 

堆棧跟蹤

org.springframework.dao.InvalidDataAccessApiUsageException: Found named stored procedure parameter associated with positional parameters; nested exception is java.lang.IllegalStateException: Found named stored procedure parameter associated with positional parameters 
at org.springframework.orm.jpa.EntityManagerFactoryUtils.convertJpaAccessExceptionIfPossible(EntityManagerFactoryUtils.java:381) ~[spring-orm-4.3.11.RELEASE.jar:4.3.11.RELEASE] 
at org.springframework.orm.jpa.vendor.HibernateJpaDialect.translateExceptionIfPossible(HibernateJpaDialect.java:246) ~[spring-orm-4.3.11.RELEASE.jar:4.3.11.RELEASE] 
at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.translateExceptionIfPossible(AbstractEntityManagerFactoryBean.java:488) ~[spring-orm-4.3.11.RELEASE.jar:4.3.11.RELEASE] 
at org.springframework.dao.support.ChainedPersistenceExceptionTranslator.translateExceptionIfPossible(ChainedPersistenceExceptionTranslator.java:59) ~[spring-tx-4.3.11.RELEASE.jar:4.3.11.RELEASE] 
at org.springframework.dao.support.DataAccessUtils.translateIfNecessary(DataAccessUtils.java:213) ~[spring-tx-4.3.11.RELEASE.jar:4.3.11.RELEASE] 
at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:147) ~[spring-tx-4.3.11.RELEASE.jar:4.3.11.RELEASE] 
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) ~[spring-aop-4.3.11.RELEASE.jar:4.3.11.RELEASE] 
at org.springframework.data.jpa.repository.support.CrudMethodMetadataPostProcessor$CrudMethodMetadataPopulatingMethodInterceptor.invoke(CrudMethodMetadataPostProcessor.java:133) ~[spring-data-jpa-1.11.7.RELEASE.jar:na] 
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) ~[spring-aop-4.3.11.RELEASE.jar:4.3.11.RELEASE] 
at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:92) ~[spring-aop-4.3.11.RELEASE.jar:4.3.11.RELEASE] 
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) ~[spring-aop-4.3.11.RELEASE.jar:4.3.11.RELEASE] 
at org.springframework.data.repository.core.support.SurroundingTransactionDetectorMethodInterceptor.invoke(SurroundingTransactionDetectorMethodInterceptor.java:57) ~[spring-data-commons-1.13.7.RELEASE.jar:na] 
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) ~[spring-aop-4.3.11.RELEASE.jar:4.3.11.RELEASE] 
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:213) ~[spring-aop-4.3.11.RELEASE.jar:4.3.11.RELEASE] 
at com.sun.proxy.$Proxy210.nearByEntities(Unknown Source) ~[na:na] 
at com.onwards.LocationEngine.business.EntityMovementBusinessImpl.findNearByEntities(EntityMovementBusinessImpl.java:38) ~[classes/:0.0.1-SNAPSHOT] 
at com.onwards.LocationEngine.business.EntityMovementBusinessImpl$$FastClassBySpringCGLIB$$99567b2c.invoke(<generated>) ~[classes/:0.0.1-SNAPSHOT] 
at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204) ~[spring-core-4.3.11.RELEASE.jar:4.3.11.RELEASE] 
at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:738) ~[spring-aop-4.3.11.RELEASE.jar:4.3.11.RELEASE] 
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157) ~[spring-aop-4.3.11.RELEASE.jar:4.3.11.RELEASE] 
at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:99) ~[spring-tx-4.3.11.RELEASE.jar:4.3.11.RELEASE] 
at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:282) ~[spring-tx-4.3.11.RELEASE.jar:4.3.11.RELEASE] 
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:96) ~[spring-tx-4.3.11.RELEASE.jar:4.3.11.RELEASE] 
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) ~[spring-aop-4.3.11.RELEASE.jar:4.3.11.RELEASE] 
at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:673) ~[spring-aop-4.3.11.RELEASE.jar:4.3.11.RELEASE] 
at com.onwards.LocationEngine.business.EntityMovementBusinessImpl$$EnhancerBySpringCGLIB$$b7870dee.findNearByEntities(<generated>) ~[classes/:0.0.1-SNAPSHOT] 
at com.onwards.LocationEngine.controller.EntityMovementController.findNearByEntities(EntityMovementController.java:37) ~[classes/:0.0.1-SNAPSHOT] 
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_144] 
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_144] 
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_144] 
at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_144] 
at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:205) ~[spring-web-4.3.11.RELEASE.jar:4.3.11.RELEASE] 
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:133) ~[spring-web-4.3.11.RELEASE.jar:4.3.11.RELEASE] 
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:97) ~[spring-webmvc-4.3.11.RELEASE.jar:4.3.11.RELEASE] 
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:827) ~[spring-webmvc-4.3.11.RELEASE.jar:4.3.11.RELEASE] 
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:738) ~[spring-webmvc-4.3.11.RELEASE.jar:4.3.11.RELEASE] 
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85) ~[spring-webmvc-4.3.11.RELEASE.jar:4.3.11.RELEASE] 
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:967) ~[spring-webmvc-4.3.11.RELEASE.jar:4.3.11.RELEASE] 
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:901) ~[spring-webmvc-4.3.11.RELEASE.jar:4.3.11.RELEASE] 
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970) ~[spring-webmvc-4.3.11.RELEASE.jar:4.3.11.RELEASE] 
at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:861) ~[spring-webmvc-4.3.11.RELEASE.jar:4.3.11.RELEASE] 
at javax.servlet.http.HttpServlet.service(HttpServlet.java:635) ~[servlet-api.jar:na] 
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846) ~[spring-webmvc-4.3.11.RELEASE.jar:4.3.11.RELEASE] 
at javax.servlet.http.HttpServlet.service(HttpServlet.java:742) ~[servlet-api.jar:na] 
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231) [catalina.jar:8.5.23] 
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) [catalina.jar:8.5.23] 
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52) ~[tomcat-websocket.jar:8.5.23] 
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) [catalina.jar:8.5.23] 
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) [catalina.jar:8.5.23] 
at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:99) ~[spring-web-4.3.11.RELEASE.jar:4.3.11.RELEASE] 
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) [spring-web-4.3.11.RELEASE.jar:4.3.11.RELEASE] 
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) [catalina.jar:8.5.23] 
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) [catalina.jar:8.5.23] 
at org.springframework.web.filter.HttpPutFormContentFilter.doFilterInternal(HttpPutFormContentFilter.java:108) ~[spring-web-4.3.11.RELEASE.jar:4.3.11.RELEASE] 
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) [spring-web-4.3.11.RELEASE.jar:4.3.11.RELEASE] 
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) [catalina.jar:8.5.23] 
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) [catalina.jar:8.5.23] 
at org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:81) ~[spring-web-4.3.11.RELEASE.jar:4.3.11.RELEASE] 
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) [spring-web-4.3.11.RELEASE.jar:4.3.11.RELEASE] 
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) [catalina.jar:8.5.23] 
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) [catalina.jar:8.5.23] 
at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:197) ~[spring-web-4.3.11.RELEASE.jar:4.3.11.RELEASE] 
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) [spring-web-4.3.11.RELEASE.jar:4.3.11.RELEASE] 
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) [catalina.jar:8.5.23] 
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) [catalina.jar:8.5.23] 
at org.springframework.boot.web.support.ErrorPageFilter.doFilter(ErrorPageFilter.java:115) [spring-boot-1.5.7.RELEASE.jar:1.5.7.RELEASE] 
at org.springframework.boot.web.support.ErrorPageFilter.access$000(ErrorPageFilter.java:59) [spring-boot-1.5.7.RELEASE.jar:1.5.7.RELEASE] 
at org.springframework.boot.web.support.ErrorPageFilter$1.doFilterInternal(ErrorPageFilter.java:90) [spring-boot-1.5.7.RELEASE.jar:1.5.7.RELEASE] 
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) [spring-web-4.3.11.RELEASE.jar:4.3.11.RELEASE] 
at org.springframework.boot.web.support.ErrorPageFilter.doFilter(ErrorPageFilter.java:108) [spring-boot-1.5.7.RELEASE.jar:1.5.7.RELEASE] 
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) [catalina.jar:8.5.23] 
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) [catalina.jar:8.5.23] 
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:199) [catalina.jar:8.5.23] 
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96) [catalina.jar:8.5.23] 
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:478) [catalina.jar:8.5.23] 
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:140) [catalina.jar:8.5.23] 
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:81) [catalina.jar:8.5.23] 
at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:650) [catalina.jar:8.5.23] 
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87) [catalina.jar:8.5.23] 
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:342) [catalina.jar:8.5.23] 
at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:803) [tomcat-coyote.jar:8.5.23] 
at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66) [tomcat-coyote.jar:8.5.23] 
at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:868) [tomcat-coyote.jar:8.5.23] 
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1459) [tomcat-coyote.jar:8.5.23] 
at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49) [tomcat-coyote.jar:8.5.23] 
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) [na:1.8.0_144] 
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) [na:1.8.0_144] 
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) [tomcat-util.jar:8.5.23] 
at java.lang.Thread.run(Thread.java:748) [na:1.8.0_144] 

我新的春天JPA和它的註解。該參數的名稱在@StoredProcedureParameter中明確提到,並且@param在存儲庫功能中使用了相同的名稱。這看起來像是一個非常直接的錯誤信息,因爲它表示我使用的是命名參數而不是位置參數,並且我缺少一些非常明顯的東西。但我無法在任何論壇中找到任何解決方案。 任何幫助,將不勝感激。謝謝!!

編輯 - 添加表結構

CREATE TABLE public.entity_movement ( entity bigint NOT NULL, location geography NOT NULL, movement_time timestamp with time zone NOT NULL, CONSTRAINT pk_entity PRIMARY KEY (entity), CONSTRAINT fk2sd7ux7x1atbbpdl4y0lwc9la FOREIGN KEY (entity) REFERENCES public.entity (id) MATCH SIMPLE ON UPDATE NO ACTION ON DELETE NO ACTION, CONSTRAINT fk_entity FOREIGN KEY (entity) REFERENCES public.entity (id) MATCH SIMPLE ON UPDATE NO ACTION ON DELETE CASCADE )

+0

您是否嘗試過在定義過程參數時重新排序屬性名稱?在我發現的所有例子中,它們都是「名稱,模式,類型」。在某些版本中,還存在一個與遊標相關的錯誤:https://stackoverflow.com/a/33638491/2553194似乎ParameterStrategy已被設置爲POSITIONAL而不是NAMED。希望有人能幫助你。 – RubioRic

+0

你解決了你的問題嗎?在這種情況下,請張貼解決方案,所以我們都可以學習:-) – RubioRic

+0

@RubioRic我嘗試了一整天的各種排列和組合,但沒有發生任何事情。在我嘗試時,錯誤消息一直在「您正在使用位置參數的命名參數」更改爲「返回類型是JDBC.Types.1111但註冊了JDBC.Types.2012」。這是爲了一個緊急的原型,所以我不得不求助於使用JDBC(在一個spring-boot,hibernate項目:()來完成它。所以我還沒有找到任何解決方案,我只是做了一個工作 – PriyaAnil

回答

0

你可以嘗試將名稱添加到輸出參數:

@StoredProcedureParameter(mode = ParameterMode.REF_CURSOR, name = "out", type = void.class) 
0

中爲每個JPA2彈簧數據的JPA例子。 1使用存儲過程

spring-data-examples

表示對存儲庫方法調用的兩種不同解釋,一種是與註釋元數據顯式映射,另一種從存儲庫派生過程元數據。

調用UserRepository.plus1BackedByOtherNamedStoredProcedure(...)將使用在用戶域類上聲明的元數據執行存儲過程plus1inout。

UserRepository.plus1inout(...)將從存儲庫派生存儲過程元數據,並默認爲位置參數綁定,並期望後備存儲過程的單個輸出參數。

在這裏,這可能是這樣的情況下調用nearByEntities通過從回購得到解決,這是位置?

爲了得到一個嘗試,我們可以更新名稱中標註

@NamedStoredProcedureQuery(name = "near_by_entities", 

@NamedStoredProcedureQuery(name = "EntityMovement.nearByEntities", 

隨着

@Procedure(name = "near_by_entities") 
public List<EntityMovement> nearByEntities(@Param("location")String location,@Param("radius")double radius); 

@Procedure(name = "EntityMovement.nearByEntities") 
public List<EntityMovement> nearByEntitiesNamed(@Param("location")String location,@Param("radius")double radius); 

呼叫將

List<EntityMovement> entityMovements= entityMovementRepository.nearByEntitiesNamed(location, radius); 
+0

謝謝你我會盡快澄清它是否有效 – PriyaAnil

+0

我試過了你的代碼片段,我得到以下錯誤, '類型不能爲空 \t at org.hibernate.procedure.internal.AbstractParameterRegistrationImpl。 setHibernateType(AbstractParameterRegistrationImpl.java:178)〜[hibernate-core-5.0.12.Final.jar:5.0.12.Final]' 我看到這個警告也在頂部 'ohpinternal.ProcedureCallImpl:HHH000456:Named參數用於可調用語句,但數據庫元數據指示不支持命名參數編輯 – PriyaAnil

+0

@PriyaAnil關於警告有關於hibernate社區的解釋,這些問題與正在使用的sql驅動程序有關,警告可以被忽略https://forum.hibernate.org/viewtopic.php?f=1&t= 1043165 – Rizwan

2

問題的根源是,你混合命名和位置參數。 的JPA 2.1 specification在第3.10.17.1命名的存儲過程的查詢指出,這種用法導致未定義行爲:

如果使用參數名稱參數名稱用於該參數值綁定,並提取輸出值(如果參數是INOUT或OUT參數)。如果沒有指定參數名稱,則假定使用位置參數。命名參數和位置參數的混合是不確定的。

這也可能是Hibernate--確定參數策略時僅檢查ParameterDefinition#L156中的第一個存儲過程參數的原因。

「找到與位置參數關聯的已命名的存儲過程參數」錯誤消息有點令人誤解,因爲在ProcedureCallImpl#L423中,當參數策略被命名但參數是位置的並且相反的方式時,會使用相同的錯誤消息。在你的情況下的錯誤消息應該是這樣的:「找到與命名參數關聯的位置存儲過程參數」(因爲在你的情況下,策略被定義爲named,但最後的REF_CURSOR參數是位置的)。

爲了解決這個問題,我們可以將名稱添加到REF_CURSOR參數:

@StoredProcedureParameter(mode = ParameterMode.REF_CURSOR, name = "out", type = void.class) 

但不幸的是,這將導致另一場(誤導)錯誤信息:

org.springframework.orm.jpa.JpaSystemException: PostgreSQL supports only one REF_CURSOR parameter, but multiple were registered 

儘管只有一個已註冊REF_CURSOR參數我們收到一條關於註冊多個的錯誤消息。唯一的例外是PostgresCallableStatementSupport#L66拋出,事實上其renderCallableStatement()方法包含有關要求的一些有用的信息時,規定了一個REF_CURSOR參數:

  • 它應該是第一個參數
  • 參數策略必須是位置

並且還在renderCallableStatement()方法的評論中明確指出混合命名和位置參數是不允許的。

所以我們應該刪除所提供的名稱:

@StoredProcedureParameter(mode = ParameterMode.REF_CURSOR, type = void.class), 
@StoredProcedureParameter(mode = ParameterMode.IN, type = String.class), 
@StoredProcedureParameter(mode = ParameterMode.IN, type = Double.class) 

由於目前春節數據不支持位置參數映射的首要(僅named parameter mapping)和我們的第一個參數是一個REF_CURSOR,我們得到了以下錯誤消息當彈簧數據試圖映射REF_CURSOR到存儲庫中的接口中定義的第一種方法參數:

InvalidDataAccessApiUsageException: Parameter value [location] did not match expected type [void (n/a)] 

所以@Procedure不能再被使用,但作爲一種解決方法,我們可以創建並實施一個單獨的EntityMovementRepositoryWithProcedure接口和做手工映射:調用存儲過程時,否則

public interface EntityMovementRepositoryWithProcedure { 
    List<EntityMovement> nearByEntities(String location, double radius); 
} 

@Repository 
public interface EntityMovementRepository extends JpaRepository<EntityMovement, Integer>, EntityMovementRepositoryWithProcedure { } 

public class EntityMovementRepositoryImpl implements EntityMovementRepositoryWithProcedure { 

    @PersistenceContext 
    private EntityManager em; 

    @Override 
    public List<EntityMovement> nearByEntities(String location, double radius) { 
     StoredProcedureQuery nearByEntities em.createNamedStoredProcedureQuery("near_by_entities"); 
     nearByEntities.setParameter(2, location); 
     nearByEntities.setParameter(3, radius); 
     return nearByEntities.getResultList(); 
    } 
} 

而且autocommit has to be disabled when using PostgreSQL REF_CURSOR下,會引發異常:

PSQLException: ERROR: cursor "<unnamed portal 1>" does not exist 

一個完整的實例可以在這裏找到:https://github.com/sandor-balazs/example/tree/master/spring-data-postgresql-refcursor

+0

感謝您爲什麼錯誤即將到來的詳細說明。但是,在我遇到問題時,我已經嘗試了您在答案結尾處提出的解決方法。錯誤是'org.postgresql.util.PSQLException:執行了一個CallableStatement函數,out參數1的類型爲java.sql.Types = 1111,但是註冊了java.sql.Types = 2012的類型。建議,以確保我沒有錯過任何可能的解決方案。我不得不求助於JDBC來完成這項工作。 – PriyaAnil

+0

@PriyaAnil你可以更新你的問題,關於你正在使用的表結構的更多細節?我試圖重現你的錯誤,但我沒有得到'PSQLException'與錯誤的註冊類型相關。我還更新了我的答案,並提供了一個完整的示例鏈接。 –

+0

@SandorBalazs很好的解釋 – Rizwan