I am facing issues with Oracle Stored Procedure when I invoke it using Java Spring Boot Application.
Case 1: Works
create or replace PROCEDURE CARDMASK
(
FIRSTNAME IN VARCHAR2,
LASTNAME IN VARCHAR2,
P_EMPS OUT SYS_REFCURSOR
) AS
stmt VARCHAR2(80);
BEGIN
stmt := 'SELECT * FROM USERS';
IF FirstName IS NOT NULL AND LastName IS NOT NULL THEN
stmt := stmt || ' WHERE ' || 'FirstName=' || FirstName || ' AND LastName=' || LastName ;
END IF;
OPEN p_emps FOR stmt;
EXCEPTION
WHEN OTHERS THEN
dbms_output.put_line(SQLERRM);
END CARDMASK;
Java Implementation
@Entity
@NamedStoredProcedureQueries({@NamedStoredProcedureQuery(
name = "test",
procedureName = "CARDMASK",
resultClasses = Customer.class,
parameters = {
@StoredProcedureParameter(mode=IN, name="FIRSTNAME", type=String.class),
@StoredProcedureParameter(mode=IN, name="LASTNAME", type=String.class),
@StoredProcedureParameter(mode=REF_CURSOR, name="P_EMPS", type=void.class)
}
)})
Case 2: Fails
create or replace PROCEDURE CARDMASK
(
FIRSTNAME IN VARCHAR2,
LASTNAME IN VARCHAR2,
P_EMPS OUT SYS_REFCURSOR
) AS
stmt VARCHAR2(80);
BEGIN
stmt := 'SELECT ID,FIRSTNAME, LASTNAME, AGE, PHONE FROM USERS';
IF FirstName IS NOT NULL AND LastName IS NOT NULL THEN
stmt := stmt || ' WHERE ' || 'FirstName=' || FirstName || ' AND LastName=' || LastName ;
END IF;
OPEN p_emps FOR stmt;
EXCEPTION
WHEN OTHERS THEN
dbms_output.put_line(SQLERRM);
END CARDMASK;
Java Implementation
@NamedStoredProcedureQueries({@NamedStoredProcedureQuery(
name = "test",
procedureName = "CARDMASK",
parameters = {
@StoredProcedureParameter(mode=IN, name="FIRSTNAME", type=String.class),
@StoredProcedureParameter(mode=IN, name="LASTNAME", type=String.class),
@StoredProcedureParameter(mode=REF_CURSOR, name="P_EMPS", type=void.class)
}
)})
If I return specific columns from the Users table in Stored Procedure, Client application fails with Null Pointer Exception. I have removed the ResultClasse from the Stored procedure implementation.
Here is the error stack trace. Any pointers to solve this issue will be appreciated, thanks.
java.lang.NullPointerException: null
at org.hibernate.loader.custom.JdbcResultMetadata.<init>(JdbcResultMetadata.java:33) ~[hibernate-core-5.6.14.Final.jar:5.6.14.Final]
at org.hibernate.loader.custom.CustomLoader.autoDiscoverTypes(CustomLoader.java:488) ~[hibernate-core-5.6.14.Final.jar:5.6.14.Final]
at org.hibernate.result.internal.OutputsImpl$CustomLoaderExtension.processResultSet(OutputsImpl.java:317) ~[hibernate-core-5.6.14.Final.jar:5.6.14.Final]
at org.hibernate.result.internal.OutputsImpl.extractResults(OutputsImpl.java:140) ~[hibernate-core-5.6.14.Final.jar:5.6.14.Final]
at org.hibernate.procedure.internal.ProcedureOutputsImpl.access$500(ProcedureOutputsImpl.java:26) ~[hibernate-core-5.6.14.Final.jar:5.6.14.Final]
at org.hibernate.procedure.internal.ProcedureOutputsImpl$ProcedureCurrentReturnState.lambda$buildExtendedReturn$0(ProcedureOutputsImpl.java:95) ~[hibernate-core-5.6.14.Final.jar:5.6.14.Final]
at org.hibernate.result.internal.ResultSetOutputImpl.getResultList(ResultSetOutputImpl.java:41) ~[hibernate-core-5.6.14.Final.jar:5.6.14.Final]
at org.hibernate.procedure.internal.ProcedureCallImpl.getResultList(ProcedureCallImpl.java:724) ~[hibernate-core-5.6.14.Final.jar:5.6.14.Final]
at org.springframework.data.jpa.repository.query.JpaQueryExecution$ProcedureExecution.doExecute(JpaQueryExecution.java:329) ~[spring-data-jpa-2.7.6.jar:2.7.6]
at org.springframework.data.jpa.repository.query.JpaQueryExecution.execute(JpaQueryExecution.java:90) ~[spring-data-jpa-2.7.6.jar:2.7.6]
at org.springframework.data.jpa.repository.query.AbstractJpaQuery.doExecute(AbstractJpaQuery.java:156) ~[spring-data-jpa-2.7.6.jar:2.7.6]
at org.springframework.data.jpa.repository.query.AbstractJpaQuery.execute(AbstractJpaQuery.java:144) ~[spring-data-jpa-2.7.6.jar:2.7.6]
at org.springframework.data.repository.core.support.RepositoryMethodInvoker.doInvoke(RepositoryMethodInvoker.java:137) ~[spring-data-commons-2.7.6.jar:2.7.6]
at org.springframework.data.repository.core.support.RepositoryMethodInvoker.invoke(RepositoryMethodInvoker.java:121) ~[spring-data-commons-2.7.6.jar:2.7.6]
at org.springframework.data.repository.core.support.QueryExecutorMethodInterceptor.doInvoke(QueryExecutorMethodInterceptor.java:160) ~[spring-data-commons-2.7.6.jar:2.7.6]
at org.springframework.data.repository.core.support.QueryExecutorMethodInterceptor.invoke(QueryExecutorMethodInterceptor.java:139) ~[spring-data-commons-2.7.6.jar:2.7.6]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) ~[spring-aop-5.3.24.jar:5.3.24]
at org.springframework.data.projection.DefaultMethodInvokingMethodInterceptor.invoke(DefaultMethodInvokingMethodInterceptor.java:81) ~[spring-data-commons-2.7.6.jar:2.7.6]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) ~[spring-aop-5.3.24.jar:5.3.24]
at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:123) ~[spring-tx-5.3.24.jar:5.3.24]
at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:388) ~[spring-tx-5.3.24.jar:5.3.24]
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:119) ~[spring-tx-5.3.24.jar:5.3.24]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) ~[spring-aop-5.3.24.jar:5.3.24]
at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:137) ~[spring-tx-5.3.24.jar:5.3.24]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) ~[spring-aop-5.3.24.jar:5.3.24]
at org.springframework.data.repository.core.support.SurroundingTransactionDetectorMethodInterceptor.invoke(SurroundingTransactionDetectorMethodInterceptor.java:61) ~[spring-data-commons-2.7.6.jar:2.7.6]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) ~[spring-aop-5.3.24.jar:5.3.24]
at org.springframework.data.jpa.repository.support.CrudMethodMetadataPostProcessor$CrudMethodMetadataPopulatingMethodInterceptor.invoke(CrudMethodMetadataPostProcessor.java:145) ~[spring-data-jpa-2.7.6.jar:2.7.6]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) ~[spring-aop-5.3.24.jar:5.3.24]
at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:97) ~[spring-aop-5.3.24.jar:5.3.24]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) ~[spring-aop-5.3.24.jar:5.3.24]
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:215) ~[spring-aop-5.3.24.jar:5.3.24]
at com.sun.proxy.$Proxy117.findCustomers(Unknown Source) ~[na:na]
at com.storedproc.demo.oracle.OracleService.findCustomers(OracleService.java:31) ~[classes/:na]
at com.storedproc.demo.oracle.OracleService$$FastClassBySpringCGLIB$$1ebc6a0a.invoke(<generated>) ~[classes/:na]
at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:218) ~[spring-core-5.3.24.jar:5.3.24]
at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:793) ~[spring-aop-5.3.24.jar:5.3.24]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:163) ~[spring-aop-5.3.24.jar:5.3.24]
at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.proceed(CglibAopProxy.java:763) ~[spring-aop-5.3.24.jar:5.3.24]
at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:123) ~[spring-tx-5.3.24.jar:5.3.24]
at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:388) ~[spring-tx-5.3.24.jar:5.3.24]
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:119) ~[spring-tx-5.3.24.jar:5.3.24]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) ~[spring-aop-5.3.24.jar:5.3.24]
at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.proceed(CglibAopProxy.java:763) ~[spring-aop-5.3.24.jar:5.3.24]
at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:708) ~[spring-aop-5.3.24.jar:5.3.24]
at com.storedproc.demo.oracle.OracleService$$EnhancerBySpringCGLIB$$b2179007.findCustomers(<generated>) ~[classes/:na]
at com.storedproc.demo.DatabaseRest.getCustomer(DatabaseRest.java:28) ~[classes/:na]
at sun.reflect.GeneratedMethodAccessor21.invoke(Unknown Source) ~[na:na]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_311]
at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_311]
at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:205) ~[spring-web-5.3.24.jar:5.3.24]
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:150) ~[spring-web-5.3.24.jar:5.3.24]
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:117) ~[spring-webmvc-5.3.24.jar:5.3.24]
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:895) ~[spring-webmvc-5.3.24.jar:5.3.24]
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:808) ~[spring-webmvc-5.3.24.jar:5.3.24]
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87) ~[spring-webmvc-5.3.24.jar:5.3.24]
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1071) ~[spring-webmvc-5.3.24.jar:5.3.24]
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:964) ~[spring-webmvc-5.3.24.jar:5.3.24]
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1006) ~[spring-webmvc-5.3.24.jar:5.3.24]
at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:898) ~[spring-webmvc-5.3.24.jar:5.3.24]
at javax.servlet.http.HttpServlet.service(HttpServlet.java:670) ~[tomcat-embed-core-9.0.69.jar:4.0.FR]
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:883) ~[spring-webmvc-5.3.24.jar:5.3.24]
at javax.servlet.http.HttpServlet.service(HttpServlet.java:779) ~[tomcat-embed-core-9.0.69.jar:4.0.FR]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:227) ~[tomcat-embed-core-9.0.69.jar:9.0.69]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162) ~[tomcat-embed-core-9.0.69.jar:9.0.69]
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53) ~[tomcat-embed-websocket-9.0.69.jar:9.0.69]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:189) ~[tomcat-embed-core-9.0.69.jar:9.0.69]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162) ~[tomcat-embed-core-9.0.69.jar:9.0.69]
at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:100) ~[spring-web-5.3.24.jar:5.3.24]
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:117) ~[spring-web-5.3.24.jar:5.3.24]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:189) ~[tomcat-embed-core-9.0.69.jar:9.0.69]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162) ~[tomcat-embed-core-9.0.69.jar:9.0.69]
at org.springframework.web.filter.FormContentFilter.doFilterInternal(FormContentFilter.java:93) ~[spring-web-5.3.24.jar:5.3.24]
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:117) ~[spring-web-5.3.24.jar:5.3.24]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:189) ~[tomcat-embed-core-9.0.69.jar:9.0.69]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162) ~[tomcat-embed-core-9.0.69.jar:9.0.69]
at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:201) ~[spring-web-5.3.24.jar:5.3.24]
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:117) ~[spring-web-5.3.24.jar:5.3.24]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:189) ~[tomcat-embed-core-9.0.69.jar:9.0.69]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162) ~[tomcat-embed-core-9.0.69.jar:9.0.69]
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:177) ~[tomcat-embed-core-9.0.69.jar:9.0.69]
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:97) [tomcat-embed-core-9.0.69.jar:9.0.69]
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:541) [tomcat-embed-core-9.0.69.jar:9.0.69]
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:135) [tomcat-embed-core-9.0.69.jar:9.0.69]
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92) [tomcat-embed-core-9.0.69.jar:9.0.69]
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:78) [tomcat-embed-core-9.0.69.jar:9.0.69]
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:360) [tomcat-embed-core-9.0.69.jar:9.0.69]
at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:399) [tomcat-embed-core-9.0.69.jar:9.0.69]
at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:65) [tomcat-embed-core-9.0.69.jar:9.0.69]
at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:891) [tomcat-embed-core-9.0.69.jar:9.0.69]
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1784) [tomcat-embed-core-9.0.69.jar:9.0.69]
at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49) [tomcat-embed-core-9.0.69.jar:9.0.69]
at org.apache.tomcat.util.threads.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1191) [tomcat-embed-core-9.0.69.jar:9.0.69]
at org.apache.tomcat.util.threads.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:659) [tomcat-embed-core-9.0.69.jar:9.0.69]
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) [tomcat-embed-core-9.0.69.jar:9.0.69]
at java.lang.Thread.run(Thread.java:748) [na:1.8.0_311]
CodePudding user response:
Code 1 works? I don't think so.
Sample table:
SQL> SELECT * FROM users;
ID FIRSTN LAST AGE PHONE
---------- ------ ---- ---------- -------
1 LITTLE FOOT 5 0123456
SQL>
Procedure:
SQL> SET SERVEROUTPUT ON
SQL>
SQL> CREATE OR REPLACE PROCEDURE CARDMASK (FIRSTNAME IN VARCHAR2,
2 LASTNAME IN VARCHAR2,
3 P_EMPS OUT SYS_REFCURSOR)
4 AS
5 stmt VARCHAR2 (80);
6 BEGIN
7 stmt := 'SELECT * FROM USERS';
8
9 IF FirstName IS NOT NULL
10 AND LastName IS NOT NULL
11 THEN
12 stmt :=
13 stmt
14 || ' WHERE '
15 || 'FirstName='
16 || FirstName
17 || ' AND LastName='
18 || LastName;
19 END IF;
20
21 DBMS_OUTPUT.put_line (stmt);
22
23 OPEN p_emps FOR stmt;
24 EXCEPTION
25 WHEN OTHERS
26 THEN
27 DBMS_OUTPUT.put_line (SQLERRM);
28 END CARDMASK;
29 /
Procedure created.
Let's try it:
SQL> DECLARE
2 rc SYS_REFCURSOR;
3 BEGIN
4 cardmask ('LITTLE', 'FOOT', rc);
5 END;
6 /
SELECT * FROM USERS WHERE FirstName=LITTLE AND LastName=FOOT
ORA-00904: "FOOT": invalid identifier
PL/SQL procedure successfully completed.
SQL>
So, no - your code doesn't really work. You're missing single quotes. We'll fix it later.
As of your 2nd code (which - as you said - doesn't work) (the only difference is stmt
):
SQL> CREATE OR REPLACE PROCEDURE CARDMASK (FIRSTNAME IN VARCHAR2,
2 LASTNAME IN VARCHAR2,
3 P_EMPS OUT SYS_REFCURSOR)
4 AS
5 stmt VARCHAR2 (80);
6 BEGIN
7 stmt := 'SELECT ID,FIRSTNAME, LASTNAME, AGE, PHONE FROM USERS';
8
9 IF FirstName IS NOT NULL
10 AND LastName IS NOT NULL
11 THEN
12 stmt :=
13 stmt
14 || ' WHERE '
15 || 'FirstName='
16 || FirstName
17 || ' AND LastName='
18 || LastName;
19 END IF;
20
21 DBMS_OUTPUT.put_line (stmt);
22
23 OPEN p_emps FOR stmt;
24 EXCEPTION
25 WHEN OTHERS
26 THEN
27 DBMS_OUTPUT.put_line (SQLERRM);
28 END CARDMASK;
29 /
Procedure created.
Testing:
SQL> DECLARE
2 rc SYS_REFCURSOR;
3 BEGIN
4 cardmask ('LITTLE', 'FOOT', rc);
5 END;
6 /
ORA-06502: PL/SQL: numeric or value error: character string buffer too small
PL/SQL procedure successfully completed.
SQL>
A different error (because we didn't even reach missing single quotes). What's wrong now? stmt
is too small; the whole statement doesn't fit 80 characters - enlarge it!
When fixed (both stmt
size and missing single quotes), code works:
SQL> CREATE OR REPLACE PROCEDURE CARDMASK (FIRSTNAME IN VARCHAR2,
2 LASTNAME IN VARCHAR2,
3 P_EMPS OUT SYS_REFCURSOR)
4 AS
5 stmt VARCHAR2 (200);
6 BEGIN
7 stmt := 'SELECT ID,FIRSTNAME, LASTNAME, AGE, PHONE FROM USERS';
8
9 IF FirstName IS NOT NULL
10 AND LastName IS NOT NULL
11 THEN
12 stmt :=
13 stmt
14 || ' WHERE '
15 || 'FirstName='
16 || CHR (39)
17 || FirstName
18 || CHR (39)
19 || ' AND LastName='
20 || CHR (39)
21 || LastName
22 || CHR (39);
23 END IF;
24
25 DBMS_OUTPUT.put_line (stmt);
26
27 OPEN p_emps FOR stmt;
28 EXCEPTION
29 WHEN OTHERS
30 THEN
31 DBMS_OUTPUT.put_line (SQLERRM);
32 END CARDMASK;
33 /
Procedure created.
Testing:
SQL> DECLARE
2 rc SYS_REFCURSOR;
3 BEGIN
4 cardmask ('LITTLE', 'FOOT', rc);
5 END;
6 /
SELECT ID,FIRSTNAME, LASTNAME, AGE, PHONE FROM USERS WHERE FirstName='LITTLE' AND LastName='FOOT'
PL/SQL procedure successfully completed.
SQL>
That's fine as far as Oracle is concerned. I can't assist with Java, I don't speak that language.