本文介绍了Hibernate HQL NULL比较模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

想象一个简单的网页,您可以像这样搜索预订:






  • 如果预订号字段已填充,我要返回所有行。

  • 如果预订号字段以返回该行。



我在Hibernate中使用了一个模式(使用底层的MySQL数据库) :

  AND(:bookingNum IS NULL或(:bookingNum IS NOT NULL AND:bookingNum = booking.bookingNumber))

我最近看到一些,让我想知道是否有更好的模式在这里使用。我肯定非Hibernate查询必须有类似的问题。



其他人在这种情况下做什么?



谢谢,



D。

解决方案

使用Hibernate条件查询,仅当bookingNum不为null时才添加谓词。 / p>

Imagine a simple web page allowing you to search Bookings like this:

picture of web page http://locuslive.com/webdrive/booking.png

  • If the Booking Number field is not populated I want to return all rows.
  • If the Booking Number field is populated I only want to return that row.

I have been using a pattern in Hibernate (with an underlying MySQL database) to accomplish this that goes something like:

AND (:bookingNum IS NULL OR (:bookingNum IS NOT NULL AND :bookingNum = booking.bookingNumber))

I've recently been seeing some weird performance issues and that got me wondering if there is a better pattern to use here. I'm sure non-Hibernate queries must have similar issues.

What do others do in this siutation?

Thanks,

D.

解决方案

Use a Hibernate criteria query, adding the predicate only if bookingNum isn't null.

这篇关于Hibernate HQL NULL比较模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-22 14:07