-->
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.  [ 8 posts ] 
Author Message
 Post subject: Criteria API, Order and nested properties
PostPosted: Wed Aug 09, 2006 1:05 pm 
Newbie

Joined: Mon Jul 31, 2006 8:32 am
Posts: 7
Hi there,

Let's assume that I've defined following entities parent and child with one-to-many bi-directional relationschip.

Code:
@Entity(name="parent")
public class Parent {

   @Id @GeneratedValue
   private Long id;
   private String name;
   @OneToMany
   private Set<Child> children;

  /* getters and setters */

}


Code:
@Entity(name="child")
public class Child {

   @Id @GeneratedValue
   private Long id;
   private String name;
   @ManyToOne
   private Parent parent;

  /* getters and setters */

}


When I run HQL guery:
Code:
session.createQuery("from child order by parent.name").list()

I get list of Child objects sorted by theirs parents' name.

But, when I run Criteria API that represents basically the same query:
Code:
session.createCriteria(Child.class).addOrder(Order.asc('parent.name').list()

I get exception instead of nice list:
Code:
org.hibernate.QueryException: could not resolve property: parent.name of: Child
   at org.hibernate.persister.entity.AbstractPropertyMapping.throwPropertyException(AbstractPropertyMapping.java:43)
   at org.hibernate.persister.entity.AbstractPropertyMapping.toColumns(AbstractPropertyMapping.java:63)
   at org.hibernate.persister.entity.BasicEntityPropertyMapping.toColumns(BasicEntityPropertyMapping.java:31)
   at org.hibernate.persister.entity.AbstractEntityPersister.toColumns(AbstractEntityPersister.java:1294)
   at org.hibernate.loader.criteria.CriteriaQueryTranslator.getColumns(CriteriaQueryTranslator.java:434)
   at org.hibernate.loader.criteria.CriteriaQueryTranslator.getColumnsUsingProjection(CriteriaQueryTranslator.java:394)
   at org.hibernate.criterion.Order.toSqlString(Order.java:45)
   at org.hibernate.loader.criteria.CriteriaQueryTranslator.getOrderBy(CriteriaQueryTranslator.java:348)
   at org.hibernate.loader.criteria.CriteriaJoinWalker.<init>(CriteriaJoinWalker.java:82)
   at org.hibernate.loader.criteria.CriteriaLoader.<init>(CriteriaLoader.java:68)
   at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1543)
   at org.hibernate.impl.CriteriaImpl.list(CriteriaImpl.java:283)


I'm a bit confused. Can you explain whay it doesn't work? I was absolutely sure that Hibernate can handle nested properties in order clause of Criteria API queries.

I use Hibernate 3.2.0 RC2 with Hibernate Annotations 3.2.0 RC1

Thanks a lot,
Tom


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 09, 2006 1:13 pm 
Beginner
Beginner

Joined: Wed Jan 25, 2006 10:18 am
Posts: 28
I've run into this myself. You'll need to create an alias:

Code:
criteria.createAlias("yourProperty", "yP");


and then use Order.asc("yP.something")


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 09, 2006 1:14 pm 
Expert
Expert

Joined: Tue Apr 25, 2006 12:04 pm
Posts: 260
modify your code as below

Code:
session.createCriteria(Child.class).createAlias( "parent", "p" ).addOrder(Order.asc("p.name").list()


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 09, 2006 1:19 pm 
Expert
Expert

Joined: Thu Sep 22, 2005 10:29 am
Posts: 285
Location: Almassera/Valencia/Spain/EU/Earth/Solar system/Milky Way/Local Group/Virgo Supercluster
Try with:
Code:
session.createCriteria(Child.class)
   .createAlias("parent.name", "parentName")
   .addOrder(Order.asc("parentName"))
   .list();


Or with:
Code:
session.createCriteria(Child.class)
   .createCriteria("parent", "p")
      .addOrder(Order.asc("name"))
   .list();


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 10, 2006 6:59 am 
Newbie

Joined: Mon Jul 31, 2006 8:32 am
Posts: 7
OK guys. Thanks a million for the advice with aliases and nested criterias.

It works, but I don't like the additiona lines of code I have to write ;-)

Furthermore, when I examined SQL queries got confused one more time.

HQL Query produces following SQL query:
Code:
select child.id, child.name, child.parent_id
from child, parent
where child.parent_id=parent.id
order by parent.name


Criteria API produces this one:
Code:
select child.id, child.name, child.parent_id, parent.id, parent.name
from child inner join parent on child.parent_id=parent.id
order by parent.name asc


They look very similar but there is a significant differnece. HQL-based query don't load all properties for parent object, but uses lazy loading. Criteria API query seem to initialize entire graph of objects. I've tried:
Code:
criteria.setFetchMode("parent", FetchMode.SELECT);
, but unfortunatelly it hasn't work.

Do you, guys, have and comments on this?

Cheers,
Tom


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 10, 2006 7:22 am 
Newbie

Joined: Mon Jul 31, 2006 8:32 am
Posts: 7
I'm soory for multiple post. Chhers, Tom


Top
 Profile  
 
 Post subject:
PostPosted: Mon Oct 02, 2006 12:29 pm 
Newbie

Joined: Fri Oct 14, 2005 8:37 am
Posts: 10
Location: University of Edinburgh
Have you got any solutions to this? I have a very similar problem: low performance due to my use of criteria search as opposed to HQL. Also tried SELECT fetch mode but, as you said, it does not work.

Any ideas?

Rafael

tblachowicz wrote:
OK guys. Thanks a million for the advice with aliases and nested criterias.

It works, but I don't like the additiona lines of code I have to write ;-)

Furthermore, when I examined SQL queries got confused one more time.

HQL Query produces following SQL query:
Code:
select child.id, child.name, child.parent_id
from child, parent
where child.parent_id=parent.id
order by parent.name


Criteria API produces this one:
Code:
select child.id, child.name, child.parent_id, parent.id, parent.name
from child inner join parent on child.parent_id=parent.id
order by parent.name asc


They look very similar but there is a significant differnece. HQL-based query don't load all properties for parent object, but uses lazy loading. Criteria API query seem to initialize entire graph of objects. I've tried:
Code:
criteria.setFetchMode("parent", FetchMode.SELECT);
, but unfortunatelly it hasn't work.

Do you, guys, have and comments on this?

Cheers,
Tom


Top
 Profile  
 
 Post subject: Watch out for fetch bugs in nested criteria
PostPosted: Mon Oct 02, 2006 6:28 pm 
Newbie

Joined: Mon Sep 13, 2004 9:44 am
Posts: 13
rmorales wrote:
Criteria API query seem to initialize entire graph of objects. I've tried:
Code:
criteria.setFetchMode("parent", FetchMode.SELECT);
, but unfortunatelly it hasn't work.


You probably meant FetchMode.JOIN. However,...

I have encountered problems with fetching associations of nested criteria. See http://forum.hibernate.org/viewtopic.php?t=954653 for a work-around.


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