-->
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.  [ 7 posts ] 
Author Message
 Post subject: Ordering criteria using child properties
PostPosted: Tue Sep 14, 2004 12:29 am 
Newbie

Joined: Thu Dec 11, 2003 10:06 pm
Posts: 8
Hibernate version: 2.1.4

Mapping documents:
<!-- Parent Class -->
<many-to-one
name="child"
class="Child"
not-null="true"
>
<column name="CHILD_ID" />
</many-to-one>

<!-- Child class -->
<property
name="number"
type="java.lang.Integer"
column="NUMBER"
length="9"
/>

Full stack trace of any exception that occurs:

could not resolve property: x.number of: Parent


Name and version of the database you are using: Oracle 9.1.2

Code:

Criteria query = session.createCriteria(Parent.class);
query.createAlias("child", "x");
query.addOrder(Order.desc("x.number"));
query.setFetchMode("child",FetchMode.EAGER);


I've read about doing this kind of ordering on a Criteria query as being part of 2.1.2+ and I'm using 2.1.4. Is it possible in 2.1.4 and if so am I doing this correctly?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 17, 2005 6:43 am 
Beginner
Beginner

Joined: Tue Jan 04, 2005 5:39 am
Posts: 37
Hi,

I am running into the same problem!

Does anyone knows if this feture works?

Thanks a lot!
Michael.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 02, 2005 7:02 am 
Expert
Expert

Joined: Sat Oct 25, 2003 8:49 am
Posts: 490
Location: Vrhnika, Slovenia
Looking through code it seems that it cannot be done.
Or maybe I'm mistaken?

Rgds, Ales

-----------------------------------

Used in Order.toSqlString on AbstractCriterion with EMPTY_MAP

protected static String[] getColumns(SessionFactoryImplementor sessionFactory, Class persistentClass, String property, String alias, Map aliasClasses) throws HibernateException {
if ( property.indexOf('.')>0 ) {
String root = StringHelper.root(property);
Class clazz = (Class) aliasClasses.get(root);
//since alliasClasses map is empty this is null
if (clazz!=null) {
persistentClass = clazz;
alias = root;
property = property.substring( root.length()+1 );
}
}
return getPropertyMapping(persistentClass, sessionFactory).toColumns(alias, property);
}

AbstractPropertyMapping doesn't hold nested properties (ok, association identifiers excluded), so both columns and template are null --> QueryException.

public String[] toColumns(String alias, String propertyName)
throws QueryException {

String[] columns = (String[]) columnsByPropertyPath.get(propertyName);
if (columns==null) {
String template = (String) formulaTemplatesByPropertyPath.get(propertyName);
if (template==null) {
throw new QueryException( "could not resolve property: " + propertyName + " of: " + getClassName() );
}
else {
return new String[] { StringHelper.replace(template, Template.TEMPLATE, alias) };
}
}
else {
return StringHelper.qualify(alias, columns);
}
}


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 02, 2005 7:07 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
Use latest release of HB3


Top
 Profile  
 
 Post subject: Re: Ordering criteria using child properties
PostPosted: Wed Feb 02, 2005 8:05 am 
Regular
Regular

Joined: Fri Dec 17, 2004 10:38 am
Posts: 54
fupeg wrote:
Code:

Criteria query = session.createCriteria(Parent.class);
query.createAlias("child", "x");
query.addOrder(Order.desc("x.number"));
query.setFetchMode("child",FetchMode.EAGER);


Does this code do the trick?

Code:
Criteria criteria = session.createCriteria(Parent.class);
criteria.createCriteria("child").addOrder(Order.desc("number"));


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 02, 2005 8:21 am 
Expert
Expert

Joined: Sat Oct 25, 2003 8:49 am
Posts: 490
Location: Vrhnika, Slovenia
No. You should get this exception.

Subcriteria:

public Criteria addOrder(Order order) {
throw new UnsupportedOperationException("subcriteria cannot be ordered");
}


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 02, 2005 12:45 pm 
Expert
Expert

Joined: Sat Jan 17, 2004 2:57 pm
Posts: 329
Location: In the basement in my underwear
I can attest that this fully works under Hib 3 Beta 3. It's been an annoyance for us in the past so when I saw that it might work now I decided to give it a go.

The new Criteria features are a nice surprise, I didn't expect any enhancements until 3.1


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