-->
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: Creating a dynamic query
PostPosted: Wed Mar 08, 2006 3:31 pm 
Newbie

Joined: Thu Oct 27, 2005 6:01 pm
Posts: 15
Hi all,

I have the following HQL query:

Code:
select distinct budget.clubType
from Budget budget
where budget.season.budgetYear = :budgetYear
and budget.season.seasonCode = :seasonCode


(The query is actually more complex, so I'm abbreviating here for simplicity's sake.)

The query works and does what I need. But now I would like to use the same query to retrieve clubTypes from different tables, so I need to supply the entity specified in the FROM clause dynamically. Hibernate query parser complains if I do "from :entity ent", so I guess that's not allowed. I suspect I'll need to switch to using the Criteria API instead of HQL to implement that, but I'm not sure how to do this.

Any help will be appreciated.

Eugene

P.S. I'm using Hibernate 3.1.2.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 08, 2006 4:22 pm 
Expert
Expert

Joined: Fri Aug 19, 2005 2:11 pm
Posts: 628
Location: Cincinnati
String concatenation still works

Code:

session.createQuery("select distinct " + entityName + " from " + otherVariable + " where bla bla bla");


_________________
Chris

If you were at work doing this voluntarily, imagine what you'd want to see to answer a question.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 08, 2006 4:47 pm 
Newbie

Joined: Thu Oct 27, 2005 6:01 pm
Posts: 15
Thanks Chris!

That will certainly work. But I was hoping to find a more "Hibernate-native" solution - perhaps as part of a general habit of treating each problem as a learning experience. And there is plenty of learning that I still have to do when it comes to Hibernate! :-)

Thanks again.

Eugene


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 08, 2006 6:50 pm 
Expert
Expert

Joined: Fri Aug 19, 2005 2:11 pm
Posts: 628
Location: Cincinnati
criteria work very well for dynamic queries, but I think a simple solution is sometimes the key.

Also, Criteria and query parameters happening 2 joins away are nasty.

_________________
Chris

If you were at work doing this voluntarily, imagine what you'd want to see to answer a question.


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.