Hibernate 中带有命名查询的可选参数? [英] Optional parameters with named query in Hibernate?

查看:24
本文介绍了Hibernate 中带有命名查询的可选参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在使用 休眠?我正在使用本机 SQL 查询,但该问题可能适用于已命名的 HQL 查询也是如此.

Is there any way to specify optional parameters (such as when search parameters are provided from a form and not all parameters are required) in a named query when using Hibernate? I'm using a native SQL query, but the question is probably applicable to named HQL queries as well.

我很确定答案是否定的,但我还没有在文档中找到明确的答案.

I'm pretty sure the answer to this is 'no', but I haven't found the definitive answer in the documentation yet.

推荐答案

AFAIK,没有这样的东西,所以你必须为此编写一个动态查询.也许看看这个 以前的答案 展示了如何在 HQL 中做到这一点(您可以转换为 SQL),并且还展示了 Criteria API 如何使它变得更简单,从而在我看来更适合这项工作.

AFAIK, there is no such thing so you'll have to write a dynamic query for this. Maybe have a look at this previous answer showing how to do this in HQL (that you can transpose to SQL) and also showing how the Criteria API makes it simpler and is thus better suited for this job in my opinion.

更新:(回答来自 OP 的评论)使用 Hibernate 处理遗留数据库确实很棘手.也许您可以使用动态本机查询并返回 non-管理实体.但从长远来看,事情可能会变得更糟(我不能告诉你).也许 Hibernate 不是您的最佳选择,而 iBATIS 之类的东西可以为您提供所需的灵活性.

Update: (answering a comment from the OP) Working with a legacy database can be indeed tricky with Hibernate. Maybe you can use a dynamic native query and return non-managed entities though. But on the long run, things might get worse (I can't tell that for you). Maybe Hibernate is not the best choice in your case and something like iBATIS would give you the flexibility you need.

这篇关于Hibernate 中带有命名查询的可选参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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