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

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

问题描述

当使用休眠?我正在使用原生 SQL 查询,但问题可能适用于命名为 HQL 查询。



我很肯定这个答案是'不',但我还没有在文档中找到明确的答案。

$ b $ AFAIK,没有这样的事情,所以你将不得不写一个动态查询。也许看看这个以前的答案,展示了如何在HQL(你可以转换到SQL),并且还显示了Criteria API如何使它更简单,因此更适合我的这个工作。

更新:(回答OP的评论)Hibernate使用遗留数据库确实很棘手。也许你可以使用动态原生查询并返回非管理实体。但从长远来看,事情可能会变得更糟(我不能告诉你)。也许Hibernate并不是您的案例中的最佳选择,而像iBATIS这样的产品会为您提供所需的灵活性。


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, 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.

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天全站免登陆