请为hibernate条件中的If语句提供示例 [英] Please provide example for an If statement in hibernate criteria
问题描述
我需要转换的查询是
SELECT date,product,IF(type ='msrp',amount,0)price,
IF(type!='msrp',amount,0)tax FROM productdetail group by date,product;
请在hibernate条件中正确使用 if语句。正如@JBnizet所说,这不受标准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屋!