问题
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