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: Need help in Hibernate Subquery
PostPosted: Wed Dec 21, 2011 10:19 pm 
Newbie

Joined: Wed Dec 21, 2011 10:10 pm
Posts: 1
Hello guys,

Im really desperate now, ive tried searching for a problem similar to mine but 'til now I failed.
I hope you can help me with this one.

I think this is really an easy problem but since I'm still very new to Hibernate I'm having a hard time.

So.. i have this query (with subqueries) tried in SQL Browser which worked:

Select * from NewsRoom as outside WHERE Date =
(Select max(Date) FROM
(Select Date FROM NewsRoom Where Country = "India" AND Year(Date) = 2011 AND year(Date) <= 2011 AND month(Date) >= 1 AND month(Date) <= 12 ORDER BY Date DESC) result
where result.Date<'2011-12-08');

now the problem is how to translate this in hql. ive tried putting aliases everywhere (and still not working)
like this code:
query = session.createQuery("from NewsRoom n1 where " +
"n1.Date = (Select max(n2.Date) FROM " +
"(Select n3.Date FROM NewsRoom n3 Where n3.Country >= :newsCountry AND Year(n3.Date) >= :newsYear1 AND year(n3.Date) <= :newsYear2 AND month(n3.Date) >= :newsMonth1 AND month(n3.Date) <= :newsMonth2) n2 " +
"WHERE n2.Date < :date)");
query.setParameter("date", date);
query.setParameter("newsCountry", country);
query.setParameter("newsMonth1", dateArgs[0]);
query.setParameter("newsYear1", dateArgs[1]);
query.setParameter("newsMonth2", dateArgs[2]);
query.setParameter("newsYear2", dateArgs[3]);

this is the method btw: public T retrieveNewsPrevious(Date date, String country, int[] dateArgs)

Any thoughts guys?


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.