请为hibernate条件中的If语句提供示例 [英] Please provide example for an If statement in hibernate criteria

查看:275
本文介绍了请为hibernate条件中的If语句提供示例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我需要转换的查询是

  SELECT date,product,IF(type ='msrp',amount,0)price,
IF(type!='msrp',amount,0)tax FROM productdetail group by date,product;

请在hibernate条件中正确使用 if语句这不受标准API的支持。除非您使用<$> c $ c $> NHibernate



如果您使用NHIbernate

  Projections.Conditional(
Restrictions.EqProperty(..............

但是这里是我用Hibernate的可能解决方案。
$ b $ 1您可以使用相同的查询通过使用 NativeSqlQuery 在这种情况下使用



2)或者使用HQL。有一个中调用的情况。这可能有帮助在HQL中使用CASE语句select


How do you write criteria on if condition using hibernate criteria.

My query that needs to be transformed is

SELECT date, product, IF(type = 'msrp', amount, 0) price, 
     IF(type != 'msrp', amount, 0) tax FROM productdetail group by date, product;

Please provide the right usage of if statement in hibernate criteria.

解决方案

As @JBnizet said,This is not supported by the criteria API. unless you are using NHibernate

if your using NHIbernate

  Projections.Conditional(
         Restrictions.EqProperty(..............

But here are my possible solutions with Hibernate.

1)You can use the same query By using NativeSqlQuery in such type of situations

2)Or by using HQL.There is an expression in HQL called case when.This might be helpful Using a CASE statement in HQL select

这篇关于请为hibernate条件中的If语句提供示例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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