-->
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.  [ 6 posts ] 
Author Message
 Post subject: ORDER BY column in related object
PostPosted: Fri Jul 14, 2006 7:24 am 
Regular
Regular

Joined: Fri Oct 01, 2004 2:19 am
Posts: 111
Location: Melbourne, Australia
Hibernate version: 3.1.2

Pseudo Mapping documents:

Code:
   <class name="NE">
                  <property name="name"/>
                  ... other properties ...
   </class>
   
   <class name="NetObj">
                  <property name="name" not-null="true"/>
                  <many-to-one name="ne" class="NE" not-null="true"/>

                  ... other properties ...
   </class>



I am attempting to run a Criteria query over NetObj where I would like the results
returned ordered by the name property of the related NE class.

From the reference manual:
Code:
List results = session.createCriteria(Domestic.class, "cat")
   .createAlias("kittens", "kit")
   .setProjection( Projections.projectionList()
      .add( Projections.property("cat.name"), "catName" )
      .add( Projections.property("kit.name"), "kitName" )
   )
   .addOrder( Order.asc("catName") )
   .addOrder( Order.asc("kitName") )
   .list();




When I try this code from the manual, I keep getting errors - the referenced property is unknown.

Basically I'm doing the equivalent of:
Code:
List results = session.createCriteria(NetObj.class)
   .setProjection( Projections.projectionList()
      .add( Projections.property("ne.name"))
   )
   .addOrder( Order.asc("ne.name") )
   .list();


or


List results = session.createCriteria(NetObj.class)
   .setProjection( Projections.projectionList()
      .add( Projections.property("ne"))
   )
   .addOrder( Order.asc("ne.name") )
   .list();


or

List results = session.createCriteria(NetObj.class)
   .addOrder( Order.asc("ne.name") )
   .list();




I get the same exception in both cases. Tracing into the Hibernate code,
it seems that H is not attempting to follow a path through the "ne" reference. Also, this happens whether or not I use the aliased version
for the specified entities (NetObj and NE).

Any help here would be greatly appreciated.

_________________
Cheers,

Bonny

please don't forget to rate :)


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 14, 2006 10:03 am 
Expert
Expert

Joined: Tue Apr 25, 2006 12:04 pm
Posts: 260
In your code you haven't specified 'ne' as alias. If you dont specify any alias while creating Criteria, you can use 'this' as alias.' Modifying your code as below should work.


Code:
List results = session.createCriteria( NetObj.class, "ne" )
   .setProjection( Projections.projectionList()
      .add( Projections.property("ne.name"))
   )
   .addOrder( Order.asc("ne.name") )
   .list();


or


List results = session.createCriteria( NetObj.class, "ne" )
   .setProjection( Projections.projectionList()
      .add( Projections.property("ne"))
   )
   .addOrder( Order.asc("ne.name") )
   .list();


or

List results = session.createCriteria( NetObj.class, "ne" )
   .addOrder( Order.asc("ne.name") )
   .list();


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 14, 2006 11:31 am 
Regular
Regular

Joined: Fri Oct 01, 2004 2:19 am
Posts: 111
Location: Melbourne, Australia
bkmr_77,

I think there may be some misunderstanding: The listing which you
have corrected is not the way I intend to have the query run.

The Criteria object is given the NetObj class, but I want to use the name
property of the associated NE object's name property. Within NetObj, this
is represented by the many-to-one association named 'ne'. So, I think
the alias should have nothing to do with the NetObj. In fact, the ne.name
property I specified was to get the path to the associated object's name
property.

Also, in my listings, I think I've listed a combination where I explicitly set
an alias for the Criteria class as well as for the selected attribute to no
avail.

_________________
Cheers,

Bonny

please don't forget to rate :)


Top
 Profile  
 
 Post subject:
PostPosted: Sun Jul 16, 2006 7:31 pm 
Regular
Regular

Joined: Fri Oct 01, 2004 2:19 am
Posts: 111
Location: Melbourne, Australia
Can anyone help?

_________________
Cheers,

Bonny

please don't forget to rate :)


Top
 Profile  
 
 Post subject:
PostPosted: Sun Jul 16, 2006 7:53 pm 
Newbie

Joined: Wed Oct 19, 2005 8:04 am
Posts: 19
My last post is the same problem, and i got help!
Read this post:
http://forum.hibernate.org/viewtopic.php?t=962105


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jul 17, 2006 9:32 am 
Regular
Regular

Joined: Fri Oct 01, 2004 2:19 am
Posts: 111
Location: Melbourne, Australia
vadim,

vadimv wrote:
My last post is the same problem, and i got help!
Read this post:
http://forum.hibernate.org/viewtopic.php?t=962105


Yes, I figured it out eventually. I did not see how an alias could
be related to an association, but looking at the source code it's
plain enough. Maybe this needs to be added to the doco.

_________________
Cheers,

Bonny

please don't forget to rate :)


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