User defined PostgreSQL function in hibernate dialect throws exception

蓝咒 提交于 2021-02-10 15:10:33

问题


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)"));

回答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.



来源:https://stackoverflow.com/questions/62567915/user-defined-postgresql-function-in-hibernate-dialect-throws-exception

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!