How to call oracle function from spring boot application?

Harish Bagora :

My requirement is i need to call oracle function from spring boot application without using NATIVE query.

below is my oracle function which is taking Date as input

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 :-

 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.

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

Harish Bagora :

I am able to solve my issue .

step 1 :- i created custom dialect, below is my code..

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

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

# 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.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324384&siteId=1