-->
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.  [ 13 posts ] 
Author Message
 Post subject: createQuery question
PostPosted: Sun Sep 24, 2006 11:56 am 
Beginner
Beginner

Joined: Sun Sep 24, 2006 11:48 am
Posts: 21
Here is my database model:

LocalArea,WideArea, Country

The relationship:

LocalArea references a WideArea

WideArea references Country

I want to create a query that will return all LocalAreas of a certain country.

Criteria c = session.createCriteria(LocalArea.class);
c.add(Restrictions.eq("country", country));

but this can not work...so how do you solve this problem?

Thanks!


Top
 Profile  
 
 Post subject:
PostPosted: Mon Sep 25, 2006 1:43 pm 
Beginner
Beginner

Joined: Sun Sep 24, 2006 11:48 am
Posts: 21
I have tried SQLQuery now instead:

Code:
SQLQuery query = session.createSQLQuery("SELECT localArea.* from LocalArea localArea"
            + " JOIN WideArea wideArea join Country country "
            + " on wideArea.wideAreaID=localArea.wideAreaID and country.countryID=wideArea.countryID " +
                  " and country.countryID=70 and localArea.localAreaURL='" + localArea+"'");

   query.setMaxResults(1);
   query.setCacheable(true);
   localArea area = (LocalArea)query.uniqueResult();
   
            


But this strangely causes a ClasCastException....although i checked
the consistence of the table and the class - and it does work for simple queries on LocalArea without joins like that.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 26, 2006 8:09 am 
Beginner
Beginner

Joined: Sun Sep 24, 2006 11:48 am
Posts: 21
is my question too easy or too difficult? ;-)


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 26, 2006 8:17 am 
Regular
Regular

Joined: Mon May 08, 2006 6:00 am
Posts: 53
Location: India
use the association feature friend, nice e.g of that are given on this page

http://www.amicabile.com/hybernate/hybe ... pter7.html

in short, define the relation between tables in your hbm files and then use association to write a query in order to get the deisired result....

also your query is not that complex, its very common

Sudhir


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 26, 2006 9:35 am 
Beginner
Beginner

Joined: Sun Sep 24, 2006 11:48 am
Posts: 21
thanks! finally a reply ;-)


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 26, 2006 9:47 am 
Beginner
Beginner

Joined: Sun Sep 24, 2006 11:48 am
Posts: 21
...however i could not find any example of such a criteria query on that website.. :-(


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 26, 2006 9:51 am 
Beginner
Beginner

Joined: Sun Sep 24, 2006 11:48 am
Posts: 21
...and in the hibernate docs there is only an example about cats and kittens....


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 26, 2006 10:35 am 
Expert
Expert

Joined: Fri Aug 19, 2005 2:11 pm
Posts: 628
Location: Cincinnati
can your relationship be set up as 1:m from country -> wide area, and 1:m from wide area -> local area?

_________________
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: Tue Sep 26, 2006 2:10 pm 
Beginner
Beginner

Joined: Sun Sep 24, 2006 11:48 am
Posts: 21
kochcp wrote:
can your relationship be set up as 1:m from country -> wide area, and 1:m from wide area -> local area?


yes....that is how it is set up...the question is how criteria queries work if you "skip" a table in between 2 tables.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 26, 2006 2:20 pm 
Expert
Expert

Joined: Fri Aug 19, 2005 2:11 pm
Posts: 628
Location: Cincinnati
Criteria c = createCriteria(Country.class)
c.createAlias("wideArea", "wa");
c.createAlias("wa.localArea", "la");
c.add(Restrictions.eq("la.id", variable);

_________________
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: Tue Sep 26, 2006 4:54 pm 
Beginner
Beginner

Joined: Sun Sep 24, 2006 11:48 am
Posts: 21
kochcp wrote:
Criteria c = createCriteria(Country.class)
c.createAlias("wideArea", "wa");
c.createAlias("wa.localArea", "la");
c.add(Restrictions.eq("la.id", variable);



Thanks! However I would like to find a LocalArea that belong to a Country,
ignoring the WideArea.

In SQL I would do something like:

Code:
select localArea.* from LocalArea as localArea
join WideArea as wideArea,
join Country as country

on localArea.wideAreaID=wideArea.id
and wideArea.countryID=country.id

and localArea.url='some_region'
and country.id=70;


In an ideal world this would be something like:

Code:
Criteria c = session.createCriteria(LocalArea.class);
c.add(Restrictions.eq("localArea.url", "some_region"));
c.add(Restrictions.eq("wideArea", Hibernate.ANY));
c.add(Restrictions.eq("wideArea.country.countryID", 70);


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 26, 2006 5:06 pm 
Beginner
Beginner

Joined: Sun Sep 24, 2006 11:48 am
Posts: 21
Code:
Criteria crit = session.createCriteria(LocalArea.class);
      
crit.createAlias("wideArea", "w");
crit.createAlias("w.country", "c");
crit.add(Restrictions.eq("c.id", new Integer(70)));
      


okay solved it ;-)

always thought aliases where useless aliases ;-)


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 26, 2006 5:06 pm 
Expert
Expert

Joined: Fri Aug 19, 2005 2:11 pm
Posts: 628
Location: Cincinnati
Criteria c = createCriteria(Country.class)
c.add(Restrictions.eq("id", variable1);
c.createAlias("wideArea", "wa");
c.createAlias("wa.localArea", "la");
c.add(Restrictions.eq("la.url", variable2);

_________________
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.  [ 13 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.