-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: Hibernate HQL - Use CASE WHEN in where Statement
PostPosted: Mon Aug 27, 2012 6:24 am 
Newbie

Joined: Mon Aug 27, 2012 5:46 am
Posts: 2
I am trying to port a MySQL Query that works to Hibernate HQL
My MySQL query was like this:
select * from t_user where 1=1 and (case when state='1' then create_time between '2010-01-01' and '2012-01-01' else 1=2 end );


public class User
{
private String state;
private String name;
private String createTime;
//get set
}

It works and I ported it to this (HQL):
select * from User where 1=1 and (case when state='1' then createTime between '2010-01-01' and '2012-12-31' when state='2' then createTime between '2011-01-01' and '2012-01-01' else 1=2 end )
hibernate3.HibernateQueryException:
unexpected token: = near line 1, column 126 [
select u from com.demo.model.User u where 1=1 and (case when u.state='1' then u.createTime between '2010-01-01' and '2012-12-31' when state='2' then createTime between '2011-01-01' and '2012-01-01' else 1=2 end )]

[unexpected token: =] [=]error? ok I am trying Modify it

select * from User where 1=1 and (case when state like '1' then createTime between '2010-01-01' and '2012-12-31' when state like '2' then createTime between '2011-01-01' and '2012-01-01' else 1=2 end

orm.hibernate3.HibernateQueryException: unexpected token: between near line 1, column 142

t-sql language support in where conditions use case when
Is there a way to do this in HQL? Is there another better way, like restructuring the tables, what is an experts opinion on this?


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.