-->
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.  [ 5 posts ] 
Author Message
 Post subject: Trying to filter on joined subclass using Criteria API
PostPosted: Fri Mar 04, 2005 5:23 am 
Newbie

Joined: Fri Mar 04, 2005 4:04 am
Posts: 7
Location: Switzerland
Hi,

I'm trying to build a query using the Criteria API that can filter results on a joined sub-class.

To illustrate the problem let's say I've got a class "Group" with a unidirectional one-to-many relationship to a class "User", this class having a subclass "ForeignUser" containing a field "country".

Using this schema, I want to retrieve all the external users - from a given group - that match a given country.

My first shot at that was :

...tx stuff...
Criteria groupFilter = session.createCriteria(Group.class);
groupFilter.add( Expression.eq("id", 1 ));
Criteria foreignUserFilter = groupFilter.createCriteria("users");
foreignUserFilter.add( Expression.eq("country","Belgium"));
List results = groupFilter.list();

In result Hibernate throws an exception saying that the class "User" has no field named "country" (...and it doesn't indeed). The problem here is that I don't see how to tell Hibernate to search for users of type ForeignUser only.

How could I achieve that, bearing in mind that :
- The (unalterable) unidirectional nature of the relationship between Group and User prevents me from starting my query with a Criteria set on class ForeignUser (because I won't be able to join to "Group" and filter on group id from there)
- I know it can be done in HQL, but I'd rather get it done with the Criteria API
- That schema is using the "table per class" hierarchy

Thanks,
Cédric


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 04, 2005 6:07 am 
Newbie

Joined: Mon Feb 28, 2005 12:53 pm
Posts: 18
This is a common question in this forum.

To access fields from an internal class you must enter the full name of
the property. In your case if ForeignUser is named fUser in your hibernate
mapping file, to access it's country property you must use :

fUser.country


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 04, 2005 6:25 am 
Newbie

Joined: Fri Mar 04, 2005 4:04 am
Posts: 7
Location: Switzerland
Hi dpmihai,

Thanks for your quick reply. In my schema "ForeignUser" is not an inner class of "User", but a sub-class.

The class inheritance has been defined in the HBM as follow :

Code:
<hibernate-mapping>
    <class name="User" table="USER" dynamic-update="false" dynamic-insert="false">

        ...field members from User...

        <subclass name="ForeignUser" dynamic-update="false" dynamic-insert="false" discriminator-value="ForeignUser">
            <property name="country" type="string" update="true" insert="true" column="COUNTRY"/>
         </subclass>
   </class>
</hibernate-mapping>


which is quite different from an internal class.

Regards,
Cédric


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 04, 2005 8:34 am 
Newbie

Joined: Mon Feb 28, 2005 12:53 pm
Posts: 18
In this case you cannot. If you want to look for ForeignUsers use a criteria
with this class. There is no way to acces country property from User.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 07, 2005 11:57 am 
Newbie

Joined: Fri Mar 04, 2005 4:04 am
Posts: 7
Location: Switzerland
For the record, I managed to do this in HQL by filtering on the subclass first ("ForeignUser") and then joining back to the "Group" class using an outer-join (aka "theta-style join" or "cartesian product").

Less effective since it requires 3 tables to do the join (where only 2 should be needed).

The HQL that returns the wanted results looks like :

Code:
select fUser
from ForeignUser fUser,
Group group
join group.users user
where group.id=:group_id
and fUser.id=user.id
and fUser.country=:country


As of now, the Criteria API doesn't support outer joins.

Cédric


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