-->
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.  [ 4 posts ] 
Author Message
 Post subject: SQL to HQL
PostPosted: Wed Feb 29, 2012 2:11 pm 
Newbie

Joined: Sun Feb 12, 2006 3:09 pm
Posts: 3
Location: Riga, Latvia
Dear All,

I'm struggling with converting PgSQL querry 2 HQL querry (Hibernate 3.3.0):

select experiment.*, count(artifactholder.*)
from experiment left outer join artifactholder on artifactholder.experiment_fk = experiment.id
where experiment.research_fk = 1
group by experiment.id
having count(artifactholder.*) < experiment.targettrialscount

Entities setup:
Research have set of experiments,
each experiment have set of trials (artifactholders) and property targetTrialsCount

SQL query returns all experiments which have smaller amount of related trials than specified by experiment.targetTrialsCount property.

I've tried different combinations of left/right outer joins, from both ends Experiment/Trial (artifactholder), but no luck.
Here is latest version (which still is does not includes HAVING clause):

select
ejoin, count(ejoin)
from
Trial as t
left outer join t.experiment as ejoin
where
ejoin.research = :research
group by
ejoin.id

Problem with this HQL is that it does not returns experiments with zero trials (artifactholders).

Any suggestions?

Thanks in advance.


Top
 Profile  
 
 Post subject: Re: SQL to HQL
PostPosted: Thu Mar 01, 2012 4:10 am 
Expert
Expert

Joined: Tue Jun 16, 2009 3:36 am
Posts: 990
Quote:
Any suggestions?


Why not using native SQL Query Api (hibernatesession.createSQL("sql")) instead of using HQL ?
Using native SQL Query Api you can reuse exactly the same sql
as you build and run directly on the database (for example in a query-tool).


Top
 Profile  
 
 Post subject: Re: SQL to HQL
PostPosted: Thu Mar 01, 2012 11:13 am 
Newbie

Joined: Sun Feb 12, 2006 3:09 pm
Posts: 3
Location: Riga, Latvia
> Why not using native SQL Query Api: well, it is an option, but I'm afraid of some differences between HSQLDB and Postgres.

Solved it in HQL:

SELECT e FROM Experiment as e WHERE size(e.trials) < e.targetTrialsCount AND e.research = :research


Top
 Profile  
 
 Post subject: Re: SQL to HQL
PostPosted: Sat Mar 03, 2012 3:36 am 
Newbie

Joined: Fri Feb 17, 2012 8:20 am
Posts: 18
Still if you have any doubts, clear here....

Hibernate Fulll tutorials


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

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.