-->
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: How to sort a query by a property of an object in a Set
PostPosted: Fri Jan 30, 2009 10:26 pm 
Newbie

Joined: Fri Sep 23, 2005 9:30 am
Posts: 7
Hibernate version:3.1

I am trying to execute HQL, so that the results are ordered by a property of an object in a set. The object model looks like this: Ad contains ContentFile contains a set of ContentFileDetails
where ContentFileDetails has a property called size. I want to order the query by the size property.

my HQL ends up looking like this (but i can't figure out what to put where the ???? is.)

Code:
from Ad ad LEFT JOIN FETCH ad.contentFile c LEFT JOIN FETCH c.detailSet d  order by ???? asc


I have tried d.size and elements(d).size, but those don't seem to work.

My mapping snips look like this:

Code:
    <class name="ContentFile" table="UPLOADED_FILES">
             ....other props   
        <set name="detailSet" inverse="true">
            <key column="UPLOADED_FILES_ID"/>
            <one-to-many class="ContentFileDetails" />
       </set>         
    </class>

    <class name="ContentFileDetails" table="UPLOADED_FILES_VERSION" lazy="true">
      ......other propsother props     
        <many-to-one name="file" outer-join="true"
         class="ContentFile" column="UPLOADED_FILES_Id" not-        <property name="size" column="Size" type="java.lang.Long" />

    </class>


Any help would be greatly appreciated!!!


Top
 Profile  
 
 Post subject:
PostPosted: Sat Jan 31, 2009 2:27 am 
Regular
Regular

Joined: Wed Apr 25, 2007 11:44 pm
Posts: 59
try this

Code:
from Ad ad LEFT JOIN FETCH ad.contentFile c LEFT JOIN FETCH c.detailSet d  order by d.detailId asc


where detailId might be the property for the Detail POJO


Top
 Profile  
 
 Post subject:
PostPosted: Sat Jan 31, 2009 7:02 am 
Newbie

Joined: Fri Sep 23, 2005 9:30 am
Posts: 7
thx for the quick reply...but i tried that and it doesn't work. the issue is that d is not an object, but the Set containing the object. Any other ideas?


Top
 Profile  
 
 Post subject:
PostPosted: Sat Jan 31, 2009 7:52 am 
Senior
Senior

Joined: Thu Jan 08, 2009 3:48 pm
Posts: 168
d.size should be right, turn on the SQL log and post what it generated by Hibernate. You can try out the SQL directly in a database client.

When you use left join fetch with collections you should add "select DISTINCT ad" to remove double entries.


Top
 Profile  
 
 Post subject:
PostPosted: Sat Jan 31, 2009 8:26 am 
Newbie

Joined: Fri Sep 23, 2005 9:30 am
Posts: 7
Thx for the tip about distinct, i have fixed that.

As to the error, it ever gets to execute the SQL, when i use d.size, and it throws this error. For other sort fields, on the ad object, it works fine.

Code:
Caused by: java.lang.StringIndexOutOfBoundsException: String index out of range: -5
   at java.lang.String.substring(String.java:1768)
   at java.lang.String.substring(String.java:1735)
   at org.hibernate.hql.CollectionSubqueryFactory.createCollectionSubquery(CollectionSubqueryFactory.java:32)
   at org.hibernate.hql.ast.tree.FromElementType.toColumns(FromElementType.java:300)
   at org.hibernate.hql.ast.tree.FromElementType.toColumns(FromElementType.java:290)
   at org.hibernate.hql.ast.tree.FromElement.toColumns(FromElement.java:390)
   at org.hibernate.hql.ast.tree.DotNode.getColumns(DotNode.java:111)
   at org.hibernate.hql.ast.tree.DotNode.initText(DotNode.java:230)
   at org.hibernate.hql.ast.tree.DotNode.resolve(DotNode.java:224)
   at org.hibernate.hql.ast.tree.FromReferenceNode.resolve(FromReferenceNode.java:94)
   at org.hibernate.hql.ast.tree.FromReferenceNode.resolve(FromReferenceNode.java:90)
   at org.hibernate.hql.ast.HqlSqlWalker.resolve(HqlSqlWalker.java:728)
   at org.hibernate.hql.antlr.HqlSqlBaseWalker.expr(HqlSqlBaseWalker.java:1216)
   at org.hibernate.hql.antlr.HqlSqlBaseWalker.orderExprs(HqlSqlBaseWalker.java:1545)
   at org.hibernate.hql.antlr.HqlSqlBaseWalker.orderClause(HqlSqlBaseWalker.java:1521)
   at org.hibernate.hql.antlr.HqlSqlBaseWalker.query(HqlSqlBaseWalker.java:622)
   at org.hibernate.hql.antlr.HqlSqlBaseWalker.selectStatement(HqlSqlBaseWalker.java:281)
   at org.hibernate.hql.antlr.HqlSqlBaseWalker.statement(HqlSqlBaseWalker.java:229)
   at org.hibernate.hql.ast.QueryTranslatorImpl.analyze(QueryTranslatorImpl.java:228)
   at org.hibernate.hql.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:160)
   at org.hibernate.hql.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:111)
   at org.hibernate.engine.query.HQLQueryPlan.<init>(HQLQueryPlan.java:77)
   at org.hibernate.engine.query.HQLQueryPlan.<init>(HQLQueryPlan.java:56)
   at org.hibernate.engine.query.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:72)
   at org.hibernate.impl.AbstractSessionImpl.getHQLQueryPlan(AbstractSessionImpl.java:133)
   at org.hibernate.impl.AbstractSessionImpl.createQuery(AbstractSessionImpl.java:112)
   at org.hibernate.impl.SessionImpl.createQuery(SessionImpl.java:1623)
   at com.netkey.platform.data.bo.AdBO.buildSearchQuery(AdBO.java:252)
   at com.netkey.platform.data.bo.AdBO.findAdListByCustomerSorted(AdBO.java:110)
   ... 46 more


Top
 Profile  
 
 Post subject:
PostPosted: Sat Jan 31, 2009 8:37 am 
Newbie

Joined: Fri Sep 23, 2005 9:30 am
Posts: 7
after thinking about this a bit, i changed the details field to something other than size, and it worked. i think Hibernate is getting confused about the size field, b/c a set has a size() function as well. Is there a way around this, maybe alias the size field as something else?


Top
 Profile  
 
 Post subject:
PostPosted: Sat Jan 31, 2009 8:53 am 
Newbie

Joined: Fri Sep 23, 2005 9:30 am
Posts: 7
i changed the field name and that fixed the issue...if there is a way to alias it to work with the size field name that would be better, but it works.

thx for all the help


Top
 Profile  
 
 Post subject:
PostPosted: Sat Jan 31, 2009 8:59 am 
Senior
Senior

Joined: Thu Jan 08, 2009 3:48 pm
Posts: 168
yeah of course :o)

just call the property something else in the mapping like detailCount and change the get/set methods accordingly

Rating appreciated


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.