如何使用 Hibernate 调用自定义数据库函数? [英] How can you call custom database functions with Hibernate?

查看:35
本文介绍了如何使用 Hibernate 调用自定义数据库函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我要在数据库中定义一些函数(可能是 Postgres 或任何其他数据库):

If I were to define some function in the database (perhaps Postgres, or any other database):

create or replace function isValidCookie(ckie);

我会从 SQL 调用它:

I would call it from SQL as:

select * from cookietable c where isValidCookie(c.cookie);

如何从 Hibernate 调用这样的自定义函数?

How can I call a custom function such as this from Hibernate?

推荐答案

如果你想在 HQL 中使用你的自定义函数,你需要在适当的 方言

If you want to use your custom function in HQL, you'll need to define it in appropriate Dialect

看看PostgreSQLDialect(或任何其他真正的)来源,你会看到一堆 registerFunction() 调用.您需要再添加一个 :-) - 用于您自己的自定义函数.

Take a look at PostgreSQLDialect (or any other, really) source, and you'll see a bunch of registerFunction() calls. You'll need to add one more :-) - for your own custom function.

然后您必须在 Hibernate 配置中指定您自己的方言.

You'll then have to specify your own dialect in Hibernate configuration.

这篇关于如何使用 Hibernate 调用自定义数据库函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆