简体   繁体   English

如何从spring boot应用程序调用oracle函数?

[英]How to call oracle function from spring boot application?

My requirement is i need to call oracle function from spring boot application without using NATIVE query. 我的要求是我需要从Spring启动应用程序调用oracle函数而不使用NATIVE查询。

below is my oracle function which is taking Date as input 下面是我的oracle函数,它将Date作为输入

create or replace FUNCTION todate(src_dt IN date) 
 RETURN date
 is
  BEGIN 
  RETURN(to_date(src_dt)); 
  END;

i was searching solution on internet but so far did't find. 我在网上搜索解决方案,但到目前为止还没找到。 people are saying some custom dialect need to create but not found any perfect step by step link. 人们说一些自定义方言需要创建,但没有找到任何完美的一步一步的链接。

below is my java code :- 下面是我的java代码: -

 Query query1 = entityManager.createQuery("select todate(ActSubT.createdDt) from ActSubT ActSubT");
    List<Object> result=query1.getResultList();

this code should run , as of now its giving error as todate is oracle function and i haven't configured anything in application.yml file. 这个代码应该运行,截至目前它给出错误,因为todate是oracle函数,我还没有在application.yml文件中配置任何东西。

below error i am getting 我得到以下错误

  java.lang.IllegalArgumentException: org.hibernate.QueryException: 
  No data type for node: 
 org.hibernate.hql.internal.ast.tree.MethodNode 
 \-[METHOD_CALL] MethodNode: '('
  +-[METHOD_NAME] IdentNode: 'todate' {originalText=todate}

Please help 请帮忙

I am able to solve my issue . 我能够解决我的问题。

step 1 :- i created custom dialect, below is my code.. 第1步: - 我创建了自定义方言,下面是我的代码..

public class CustomDialect extends Oracle12cDialect {
    public CustomDialect() {
        super();
        // CustomFunction implements SqlFunction
      //  registerFunction("custom_function", new CustomFunction());
        // or use StandardSQLFunction; useful for coalesce
        registerFunction("todate", new StandardSQLFunction("todate", StandardBasicTypes.DATE));
    }
}

Step 2 :- now i am calling todate function , below is the Java code 第2步: - 现在我调用todate函数,下面是Java代码

Query query1 = entityManager.createQuery("select function('todate',ActSubT.createdDt) from ActSubT ActSubT where ActSubT.id=1105619");

        Object resulth=query1.getSingleResult();

Step 3 :- this entry we need to put in application.poperties / application.yml 第3步: - 这个条目我们需要放入application.poperties / application.yml

# Allows Hibernate to generate SQL optimized for a particular DBMS
spring.jpa.properties.hibernate.dialect = com.sbill.app.config.CustomDialect

That's all now i can call db function with java code. 现在我可以用java代码调用db函数了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM