-->
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.  [ 3 posts ] 
Author Message
 Post subject: HQL vs Critera - Am I missing something?
PostPosted: Mon Oct 31, 2011 11:26 am 
Newbie

Joined: Fri Oct 07, 2011 10:06 am
Posts: 3
I have a simple model which is just a chain of one-to-many relationships: Country -< City -< Street
The tables are mapped as entities and are returned as Map<String, Object>.

The following test method is producing odd results:
Code:
public static void main(String[] args) {
   Session session = HibernateSessionFactory.getSession();
   
   List<Map<String, Object>> results = null;
   
   //Query using HQL and print results
   System.out.println("FROM HQL =====================");
   String hql = "from Street where City.Country.countryid = 1";      
   Query query = session.createQuery(hql);   
   results = query.list();
   for(Map<String, Object> row : results) {
      System.out.println(row);
   }
   
   //Query using Criteria and print results
   System.out.println("FROM CRITERIA ================");
   Criteria criteria = session.createCriteria("Street");
   criteria.add(Restrictions.eq("City.Country.countryid", 1));
   results = criteria.list();
   for(Map<String, Object> row : results) {
      System.out.println(row);
   }
}


The top block which uses the HQL works as expected, but the bottom block falls over:

Output:
Code:
FROM HQL =====================
{streetname=Mayfair, City=org.hibernate.proxy.map.MapProxy@2b12e7f7, $type$=Street, streetid=1}
{streetname=Park Lane, City=org.hibernate.proxy.map.MapProxy@2b12e7f7, $type$=Street, streetid=2}
{streetname=Bond Street, City=org.hibernate.proxy.map.MapProxy@663b1f38, $type$=Street, streetid=3}
{streetname=Old Kent Road, City=org.hibernate.proxy.map.MapProxy@663b1f38, $type$=Street, streetid=4}
FROM CRITERIA ================
Exception in thread "main" org.hibernate.QueryException: could not resolve property: City.Country.countryid of: Street
   at org.hibernate.persister.entity.AbstractPropertyMapping.propertyException(AbstractPropertyMapping.java:67)
   at org.hibernate.persister.entity.AbstractPropertyMapping.toColumns(AbstractPropertyMapping.java:82)
   at org.hibernate.persister.entity.BasicEntityPropertyMapping.toColumns(BasicEntityPropertyMapping.java:54)
   at org.hibernate.persister.entity.AbstractEntityPersister.toColumns(AbstractEntityPersister.java:1367)
   at org.hibernate.loader.criteria.CriteriaQueryTranslator.getColumns(CriteriaQueryTranslator.java:457)
   at org.hibernate.loader.criteria.CriteriaQueryTranslator.getColumnsUsingProjection(CriteriaQueryTranslator.java:417)
   at org.hibernate.criterion.SimpleExpression.toSqlString(SimpleExpression.java:68)
   at org.hibernate.loader.criteria.CriteriaQueryTranslator.getWhereCondition(CriteriaQueryTranslator.java:357)
   at org.hibernate.loader.criteria.CriteriaJoinWalker.<init>(CriteriaJoinWalker.java:113)
   at org.hibernate.loader.criteria.CriteriaJoinWalker.<init>(CriteriaJoinWalker.java:82)
   at org.hibernate.loader.criteria.CriteriaLoader.<init>(CriteriaLoader.java:91)
   at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1578)
   at org.hibernate.impl.CriteriaImpl.list(CriteriaImpl.java:306)
   at com.bar.foo(Main.java:33)


I can't see why the HQL can resolve City.Country.countryid but Criteria (Restrictions) can't.

Am I missing something obvious?


Top
 Profile  
 
 Post subject: Re: HQL vs Critera - Am I missing something?
PostPosted: Thu Nov 03, 2011 5:24 pm 
Beginner
Beginner

Joined: Tue Oct 26, 2010 6:12 pm
Posts: 29
Quote:
I can't see why the HQL can resolve City.Country.countryid but Criteria (Restrictions) can't.


I think you will have to navigate the association if you are using criteria.


Top
 Profile  
 
 Post subject: Re: HQL vs Critera - Am I missing something?
PostPosted: Fri Nov 04, 2011 3:52 am 
Newbie

Joined: Fri Oct 07, 2011 10:06 am
Posts: 3
Thanks for you reply!

I've solved this using aliases to navigate to the attributes:
Code:
.createAlias("City","City");
.createAlias("City.Country","City.Country");


Now I get the results I expect. :)


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 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.