Criterion Restrictions.like 以及数字和日期
我使用 Hibernate 而不是 mySQL。在 mySQL 中,您甚至可以在查询中放入 LIKE ,甚至可以对数字(如双精度)参数和日期进行查询,例如您可以编写:
select * from sillytable where field like "3.33%"
这将起作用。问题是我不能使用 Restrictions.like 在标准中执行此操作,事实上它会抛出一个异常,表示它无法将 String 转换为 Date 或 Double。
我怎样才能用它做到这一点?
I'm using Hibernate over mySQL. In mySQL you can put LIKE in queries even on number (like double) parameters and dates, for example you can write:
select * from sillytable where field like "3.33%"
and this will work. The problem is that I can't do this in criterion using Restrictions.like, in fact it throws an exception says that it can't cast a String to a Date or Double.
How can I do this with it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我发现唯一好的解决方案可以实现我想要做的事情就是使用 HQL。
Criteria 中没有明确的方法来做到这一点
I found that the only good solution that did what I meant to do was using HQL.
There is no clear way to do this in Criteria
我认为这实际上是:
在休眠中这是:
Between 定义为
(min <= expr AND expr <= max)
。因此上限必须更改为从 3.33 开始的最大值。 (警告:如果上限不可表示并且 3.34 用 3.33999... 表示,则表示可能会失败。如果您使用这个已经有效的边界执行 nextAfter,您将错过这个值。)I think this is actually:
In hibernate this is:
Between is defined as
(min <= expr AND expr <= max)
. So the upper bound has to be changed to the biggest value starting with 3.33. (Caveat: You could have a representation failure if the upper bound is not representable and 3.34 is representated with 3.33999... . You would miss this one value if you do nextAfter with this already valid bound.)如果您仍然想坚持
Hibernate
标准,请使用Restrictions.sqlRestriction
而不是Restrictions.like
,如下所述:If you still want to stick to
Hibernate
criteria useRestrictions.sqlRestriction
instead ofRestrictions.like
as explained below: