-->
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: Using Criteria instead of HQL
PostPosted: Tue Sep 14, 2004 6:30 pm 
Newbie

Joined: Tue Sep 14, 2004 5:57 pm
Posts: 6
Location: Sunnyvale
For my company's current project, I have written several queries using HQL and it works great. I have tried to move to using the Criteria and related classes, because it seems like a more elegant way to go. Most things have been easy to go from HQL to Criteria. However, I have come across a difficult problem.

I have two tables COMPUTER and MANUFACTURER. COMPUTER references MANUFACTURER (in the DB.) In the mapping files, I have the relationship only defined from COMPUTER, i.e. :

<many-to-one name="manufacturer" class="Manufacturer" not-null="true">
<column name="MANUFACTURER_ID"/>
</many-tone>

But I don't have a corresponding <set> mapping in the mapping file for Manufacturer. The query I need to do is to retrieve all the computers of a certain type (all laptops for example) but sorted by a field on the manufacturer table/object.

The SQL is easy to do: select computer_id, computer_name, manufacturer_name, manufacturer_scode
from computer c, manufacturer m where
c.manufacturer_id =m.manufacturer_id sort by manufacturer_scode

This is only for illustrative purposes of what I'm trying to do. I actually all the fields for each computer, and a couple of fields from the manufacturer table.

So my question is can this be done using a Criteria? Can it be done using HQL? I tried it as Criteria, but it did not work:

Criteria criteria = session.createCriteria(Computer.class);
criteria.add(Expression("type", new Integer(4));
criteria.addOrder(Order.asc("manufacturer.manufacturerScode");
criteria.setFetchMode("manufacturer", FetchMode.EAGER);
List results = criteria.list();


This created an error that complained that there was no property called "manufacturer.manufacturerScode" on the Computer class. Of course this is true, but there is a manufacturer property that is a Manufacturer class and that class has a property manufacturerScode. Obviously I'm doing something wrong, but can this even be done with a Criteria? Can it be done with HQL instead?

For what it's worth, I'm using Hibernate 2.1.4, Oracle 9i, Weblogic 8.1 SP2, JDK 1.4.2_04.

I did read through the many FAQs (wouldn't it be great to organize these into an interactive application...) and have searched the forums. If there is something I missed (and there probably is!) then I apologize for wasting anyone's time.


Top
 Profile  
 
 Post subject: Me 2!
PostPosted: Wed Sep 15, 2004 3:57 am 
Newbie

Joined: Mon Aug 30, 2004 9:54 am
Posts: 8
I've searched the docs for exactly the same issue, but didn't find anything. Sorting based on a property inside a many-to-one class relationship seems to be difficult. Comments, anyone?

Code:
class AClass {

   /* @hibernate.many-to-one ... */
   public BClass getBClass() {};
}

class BClass {
   public String getName() {}
}


Then I try to sort a list of AClass instances for bClass.Name, which fails. Best regards,

Christian


Top
 Profile  
 
 Post subject: same prob
PostPosted: Wed Sep 22, 2004 1:17 pm 
Newbie

Joined: Thu Oct 23, 2003 6:39 pm
Posts: 16
Hello
I have the same problem. Can anyone confirm if there is a way to do this using Criteria queries? Creating normal aliases does not seem to work, perhaps an "orderalias" is needed.
Regards
Trygve


Top
 Profile  
 
 Post subject: Works with HQL
PostPosted: Wed Sep 22, 2004 1:55 pm 
Newbie

Joined: Tue Sep 14, 2004 5:57 pm
Posts: 6
Location: Sunnyvale
I could get this to work with HQL:

String hql = "from Computer as comp where comp.type=4 "+
"order by comp.manufacturer.manufacturerScode";

This is very intuitive and works nicely. Unfortunately the analogous Criteria does not work. So I'm sticking with HQL once again!


Top
 Profile  
 
 Post subject: Can't search by association using Criteria API
PostPosted: Wed Sep 22, 2004 4:50 pm 
Beginner
Beginner

Joined: Fri Mar 26, 2004 8:19 am
Posts: 49
I've spent a lot of time on this one, but the short story is that the Criteria API will not let you do a search by example based on association fields! The only fields usable in an Example query are fields in the base class. Does this help?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Sep 22, 2004 6:24 pm 
Beginner
Beginner

Joined: Tue Sep 09, 2003 9:11 pm
Posts: 32
You might also want to check out the JIRA issue HB-132. This is a proposed patch for adding order-by support for assoications in a criteria query.

Chris....


Top
 Profile  
 
 Post subject:
PostPosted: Wed Sep 22, 2004 6:54 pm 
Senior
Senior

Joined: Wed Mar 24, 2004 11:40 am
Posts: 146
Location: Indianapolis, IN, USA
i had asked a similar question about 6 months ago and it was recommened that i use HQL for this purpose, which works great. considering how busy the development team has been with the 3.0 release i don't think this would be a high priority issue since you can accomplish this with HQL.


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.