-->
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: Using Criteria with child class and order by max
PostPosted: Tue Dec 30, 2008 3:53 pm 
Newbie

Joined: Tue Dec 30, 2008 3:44 pm
Posts: 2
I am trying to use a Criteria object to order a list of data. I tried to create a associated criteria but that didnt work. I am trying to get a list of Select * from someclass order by max (someotherclass.property) desc. Any hints would be greatly appreciated. The sql that I am getting is not even close. I am sure this is possible I just dont have the knowledge yet to do it.


Here is the mapping for the parent:
<class name="namespace.someClass" table="SomeTable">
<id name="id" type="long" column="ID">
<generator class="identity" />
</id>
<property name="caseID" column="CASEID" type="string" />
<set name="events" cascade="all" inverse="true" lazy="false"
order-by="ID asc" batch-size="1" outer-join="auto">
<key column="RFR_ID" />
<one-to-many class="someotherClass" />
</set>
</class>

Thanks
Heath


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 31, 2008 7:34 am 
Beginner
Beginner

Joined: Wed Nov 26, 2008 1:02 am
Posts: 22
hi,

in hbm.xml file after </class>, add
<query name="queryName">
<![CDATA[
from someClass reference order by reference.propertyName asc
] ]>
</query>
</hibernate-mapping>

here someClass="your pojo class"
reference="any name"
propertyName="valid propertyName",

getting List-->
List<someClass> someList=(SomeClass)session.getNamedQuery("queryName").list();


if it useful dont forget to rateme

regars,


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 20, 2009 8:21 pm 
Newbie

Joined: Tue Dec 30, 2008 3:44 pm
Posts: 2
The problem with that solution is that I am trying to use only the Criteria object.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 21, 2009 1:41 am 
Beginner
Beginner

Joined: Wed Nov 26, 2008 1:02 am
Posts: 22
hi

use this

List<someClass> someList=(SomeClass)session.createCriteria(someClassName.Class).addOrder(Order.desc(propertyNameString));

hope your problem solved.

regards,

_________________
if it solves,rate me


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 21, 2009 8:50 am 
Expert
Expert

Joined: Thu Jan 08, 2009 6:16 am
Posts: 661
Location: Germany
Actually to solve this problem, you would need a subquery in your orderBy-Part, which is not possible using criteria.

If you still want to stick with criteria, you could do something like that:
Code:
session.createCriteria(SomeClass.class).createAlias("events", "evt").addOrder(Order.desc("evt.property")).setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY).list()


This is not a really efficient solution, since it makes a join and iterates the whole resultset to get the distinct someClass-objects. Maybe anybody knows a better solution using criteria?

_________________
-----------------
Need advanced help? http://www.viada.eu


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 22, 2009 12:44 pm 
Expert
Expert

Joined: Thu Jan 08, 2009 6:16 am
Posts: 661
Location: Germany
I incidentally found a JIRA-issue, which helps solving this problem.

So you can use this subclass of order to define a custom order. The big disadvantage with this solution is, that you have to know the sql-column- and table-names.

_________________
-----------------
Need advanced help? http://www.viada.eu


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.