【问题标题】:User defined PostgreSQL function in hibernate dialect throws exception休眠方言中用户定义的 PostgreSQL 函数抛出异常
【发布时间】:2020-06-25 04:31:50
【问题描述】:

是否可以注册一个用数据库编写的自定义函数并用扩展的休眠 Postgres 方言编写如下?接收函数在HQL中使用该函数不存在异常。

Postgres 函数:

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

registerFunction("add_days", new SQLFunctionTemplate(StandardBasicTypes.DATE, "add_days(?1 , ?2)"));

【问题讨论】:

  • 能否请您提供休眠配置?
  • 任何休眠配置都可以。方言需要是扩展 Postgre 特定方言的自定义方言。对于日期字段 TemporalType.TIMESTAMP & columnDefinition 作为日期使用。
  • 好的,我会问更具体的问题:hibernate.connection.url 使用什么值?你用hibernate.default_schema吗?
  • 不使用默认架构。我会尝试你提到的解决方案。 Schema 可能会来拯救(Y)

标签: postgresql hibernate


【解决方案1】:

我也遇到过类似的问题。问题出在以下几点:

函数是在特定架构 TEST_SCHEMA 中创建的。当我使用以下配置时:

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

我明白了:

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.

但是,当我在连接 url 中明确指定默认架构时,如下所示

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

我的功能变得可见。

【讨论】:

    猜你喜欢
    • 2014-04-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-03
    • 2016-10-08
    • 2012-04-28
    • 2016-09-17
    相关资源
    最近更新 更多