1

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

7
  • Check stackoverflow.com/questions/41837546/… Commented Aug 16, 2019 at 11:52
  • @parthivrshah i went through on this answer earlier, but my requirement is without using NATIVE query i need to call db function Commented Aug 16, 2019 at 12:05
  • I don't think you can call db function without native query Commented Aug 16, 2019 at 12:09
  • @parthivrshah can you check this link thoughts-on-java.org/database-functions we have option but i am not able to configure custom dialect entry in our yml file Commented Aug 16, 2019 at 12:31
  • I never tried that. Commented Aug 16, 2019 at 12:37

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.