1

Is it possible to register a custom function written in database & written in extended hibernate Postgres Dialect as follows? Receiving function not exists exception on using this function in HQL.

Postgres function:

    create or replace function ADD_DAYS(varDate timestamp without time zone, varNumber numeric) 
     returns timestamp without time zone 
     LANGUAGE sql AS    
    $$ 
     SELECT (varDate + varNumber * INTERVAL '1 day')
    $$;

Java code:

registerFunction("add_days", new SQLFunctionTemplate(StandardBasicTypes.DATE, "add_days(?1 , ?2)"));
4
  • Could you please provide hibernate config? Commented Jun 25, 2020 at 6:06
  • Any hibernate configuration is ok with this. Dialect need be custom dialect which extends Postgre specific dialect. For the date fields TemporalType.TIMESTAMP & columnDefinition as date used. Commented Jun 25, 2020 at 9:52
  • Ok, I'll ask more specific question: what value do you use for the hibernate.connection.url? Do you use hibernate.default_schema? Commented Jun 25, 2020 at 9:57
  • Not using default schema. I'll try with solution you mentioned. Schema might come to rescue (Y) Commented Jun 26, 2020 at 12:28

1 Answer 1

1

I have faced with the similar problem. The problem was in the following:

The function was created in the particular schema TEST_SCHEMA. When I used the following configuration:

<property name="hibernate.connection.url">jdbc:postgresql://localhost:5432/postgres</property>
<property name="hibernate.default_schema">TEST_SCHEMA</property>

I got:

org.postgresql.util.PSQLException: ERROR: function levenshtein(character varying, character varying) does not exist. No function matches the given name and argument types. You might need to add explicit type casts.

But, when I specified the default schema explicitly in the connection url like below

<property name="hibernate.connection.url">jdbc:postgresql://localhost:5432/postgres?currentSchema=TEST_SCHEMA</property>

my function became visible.

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.