[英]Use PL/SQL Function in JOOQ
i need to call PL/SQL Function that return an Object Type in table form. 我需要调用以表形式返回对象类型的PL / SQL函数。 Basically like: 基本上像:
select * from TABLE(get_druck_variablen(123456);
in JOOQ im using native query so far and it works: 到目前为止,在JOOQ im中使用本机查询,它可以正常工作:
final Result<Record> result = create.fetch("select * from TABLE(myschema.get_druck_variablen(" + id + "))");
But i want to do it in JOOQ properly. 但是我想在JOOQ中正确地做。
1) I've come to this: 1)我来到这里:
final GetDruckVariablen getDruckVariablen = new GetDruckVariablen();
getDruckVariablen.setPId(id);
getDruckVariablen.execute();
but then i got this exception 但是后来我得到了这个例外
Method threw 'java.lang.NullPointerException' exception. Cannot evaluate de.company.jooq.routines.GetDruckVariablen.toString()
2) And then i've tried this: 2)然后我尝试了这个:
final TabOtDruckVariablenRecord getDruckVariablen = Routines.getDruckVariablen(create.configuration(), id).;
then i got this exception: 然后我得到了这个例外:
Caused by: org.springframework.jdbc.BadSqlGrammarException: jOOQ; bad SQL grammar [select "array_table"."COLUMN_VALUE" from table ("MYSCHEMA"."GET_DRUCK_VARIABLEN"(?)) "array_table"]; nested exception is java.sql.SQLSyntaxErrorException: ORA-00904: "array_table"."COLUMN_VALUE": ungültiger Bezeichner
For the info, here are my PL/SQL function: 有关信息,这是我的PL / SQL函数:
create or replace FUNCTION get_druck_variablen(p_id IN vk_types.t_id) RETURN myschema.tab_ot_druck_variablen
PIPELINED
IS
....
BEGIN
END get_druck_variablen;
The Object Types: 对象类型:
create or replace TYPE ot_druck_variablen FORCE AS OBJECT
(
ob_id NUMBER(9, 0),
ob_txv_name VARCHAR2(100),
ob_txv_wert VARCHAR2(2000)
)
The Table type: 表格类型:
create or replace TYPE tab_ot_druck_variablen AS TABLE OF myschema.ot_druck_variablen
I'm really hoping that someone can help me out. 我真的希望有人能帮助我。
Thanks in advance. 提前致谢。
Best regards 最好的祝福
Ivan 伊万
Added Stake traces for the answer to: 添加了放样轨迹以解决以下问题:
final GetDruckVariablen getDruckVariablen = new GetDruckVariablen();
getDruckVariablen.setPId(id);
getDruckVariablen.execute(dslContext.configuration());
Stack traces: 堆栈跟踪:
2017-08-16 22:25:16.039 ERROR 4928 --- [ restartedMain] o.s.boot.SpringApplication : Application startup failed
java.lang.IllegalStateException: Failed to execute CommandLineRunner
at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:735)
at org.springframework.boot.SpringApplication.callRunners(SpringApplication.java:716)
at org.springframework.boot.SpringApplication.afterRefresh(SpringApplication.java:703)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:304)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1118)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1107)
at de.mycompany.druckauftrag.DruckAuftragApplication.main(DruckAuftragApplication.java:31)
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:498)
at org.springframework.boot.devtools.restart.RestartLauncher.run(RestartLauncher.java:49)
Caused by: org.springframework.jdbc.BadSqlGrammarException: jOOQ; bad SQL grammar [select "array_table"."COLUMN_VALUE" from table ("MYSCHEMA"."GET_DRUCK_VARIABLEN"(?)) "array_table"]; nested exception is java.sql.SQLSyntaxErrorException: ORA-00904: "array_table"."COLUMN_VALUE": ungültiger Bezeichner
at org.jooq_3.9.3.ORACLE.debug(Unknown Source)
at org.springframework.jdbc.support.SQLErrorCodeSQLExceptionTranslator.doTranslate(SQLErrorCodeSQLExceptionTranslator.java:231)
at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:73)
at org.springframework.boot.autoconfigure.jooq.JooqExceptionTranslator.translate(JooqExceptionTranslator.java:92)
at org.springframework.boot.autoconfigure.jooq.JooqExceptionTranslator.handle(JooqExceptionTranslator.java:81)
at org.springframework.boot.autoconfigure.jooq.JooqExceptionTranslator.exception(JooqExceptionTranslator.java:54)
at org.jooq.impl.ExecuteListeners.exception(ExecuteListeners.java:245)
at org.jooq.impl.AbstractQuery.execute(AbstractQuery.java:364)
at org.jooq.impl.AbstractResultQuery.fetch(AbstractResultQuery.java:315)
at org.jooq.impl.SelectImpl.fetch(SelectImpl.java:2708)
at org.jooq.impl.AbstractRoutine.executeSelectFromOracle(AbstractRoutine.java:362)
at org.jooq.impl.AbstractRoutine.execute(AbstractRoutine.java:318)
at org.jooq.impl.AbstractRoutine.execute(AbstractRoutine.java:287)
at de.mycompany.druckauftrag.container.cask.KfzParam.getDruckVariablen(KfzParam.java:59)
at de.mycompany.druckauftrag.container.cask.KfzParam.befuelleVariablen(KfzParam.java:36)
at de.mycompany.druckauftrag.service.DokumentService.erzeugeDokument(DokumentService.java:55)
at de.mycompany.druckauftrag.service.XmlCreator.createXml(XmlCreator.java:33)
at de.mycompany.druckauftrag.DruckAuftragApplication.run(DruckAuftragApplication.java:46)
at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:732)
... 11 common frames omitted
Caused by: java.sql.SQLSyntaxErrorException: ORA-00904: "array_table"."COLUMN_VALUE": ungültiger Bezeichner
at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:450)
at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:399)
at oracle.jdbc.driver.T4C8Oall.processError(T4C8Oall.java:1059)
at oracle.jdbc.driver.T4CTTIfun.receive(T4CTTIfun.java:522)
at oracle.jdbc.driver.T4CTTIfun.doRPC(T4CTTIfun.java:257)
at oracle.jdbc.driver.T4C8Oall.doOALL(T4C8Oall.java:587)
at oracle.jdbc.driver.T4CPreparedStatement.doOall8(T4CPreparedStatement.java:225)
at oracle.jdbc.driver.T4CPreparedStatement.doOall8(T4CPreparedStatement.java:53)
at oracle.jdbc.driver.T4CPreparedStatement.executeForDescribe(T4CPreparedStatement.java:774)
at oracle.jdbc.driver.OracleStatement.executeMaybeDescribe(OracleStatement.java:925)
at oracle.jdbc.driver.OracleStatement.doExecuteWithTimeout(OracleStatement.java:1111)
at oracle.jdbc.driver.OraclePreparedStatement.executeInternal(OraclePreparedStatement.java:4798)
at oracle.jdbc.driver.OraclePreparedStatement.execute(OraclePreparedStatement.java:4901)
at oracle.jdbc.driver.OraclePreparedStatementWrapper.execute(OraclePreparedStatementWrapper.java:1385)
at sun.reflect.GeneratedMethodAccessor62.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.apache.tomcat.jdbc.pool.StatementFacade$StatementProxy.invoke(StatementFacade.java:114)
at com.sun.proxy.$Proxy94.execute(Unknown Source)
at org.jooq.tools.jdbc.DefaultPreparedStatement.execute(DefaultPreparedStatement.java:195)
at org.jooq.impl.AbstractResultQuery.execute(AbstractResultQuery.java:270)
at org.jooq.impl.AbstractQuery.execute(AbstractQuery.java:349)
... 22 common frames omitted
I suspect that you've run into a bug (that was probably fixed by now). 我怀疑您遇到了一个错误(目前可能已修复)。 As soon as I know what jOOQ version you're using, I'll update my answer with a link to the relevant GitHub issue. 知道您使用的是哪个jOOQ版本后,我将通过指向相关GitHub问题的链接来更新答案。
NullPointerException
关于1)和NullPointerException
A possible reason for the NPE in your attempt 1): 您尝试进行NPE的可能原因1):
final GetDruckVariablen getDruckVariablen = new GetDruckVariablen();
getDruckVariablen.setPId(id);
getDruckVariablen.execute();
... might be the fact that you're not passing the Configuration
to the execute()
method. ...可能是您没有将Configuration
传递给execute()
方法的事实。 The Configuration
less method assumes that your routine call has been previously "attached" to a Configuration
. “少Configuration
方法假定您的常规调用先前已“附加”到Configuration
。 But better pass it explicitly: 但最好将其明确传递:
getDruckVariablen.execute(configuration);
You're surely aware of this, but I have to mention it for future readers. 您肯定知道这一点,但我必须为以后的读者提一下。 Your usage of the jOOQ plain SQL API exposes a certain risk of SQL injection, in addition to putting pressure on the cursor cache: 使用jOOQ普通SQL API不仅会给游标缓存带来压力,还会带来SQL注入的一定风险:
final Result<Record> result = create.fetch(
"select * from TABLE(myschema.get_druck_variablen(" + id + "))");
Better to use a bind variable, which is perfectly possible with plain SQL as well: 最好使用绑定变量,这对于普通SQL也是完全可行的:
final Result<Record> result = create.fetch(
"select * from TABLE(myschema.get_druck_variablen(?))", id);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.