-->
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.  [ 2 posts ] 
Author Message
 Post subject: Criteria and filtered collections - inconsistent behavior?
PostPosted: Wed Jun 28, 2006 11:43 am 
Beginner
Beginner

Joined: Mon Jan 23, 2006 12:01 pm
Posts: 46
Hi,

I have two tables: OWNER (ownerId, name) and CAR (carId,ownerId,color,make,model).

Owner class looks like this: Owner{ownerId,name,cars}.
Car class looks like this: Car{carId,owner,color,make,model}.

As you can see, Car object has many-to-one relationship with Owner.

I create Criteria object like this:
Criteria c=session.createCriteria(Owner.class);
c.add(Expression.eq("name","John"));

And I add a filter on Car collection (name="colorFilter", param="color", condition="color=:color") like this:
session.enableFilter("colorFilter").setParameter("color","red");

As of now I have only one owner named John, so this query returns a single Owner object. John has 3 cars: red, green and blue, so Owner.cars for John contains 1 Car object (red one, according to the enabled filter).

So this works fine: Criteria are used to find top-level object (Owner), and filter is used to narrow the data in collection (Cars), since Criteria do not apply to collections.

Now I change my query like this:

Criteria c=session.createCriteria(Owner.class);
c.add(Expression.eq("name","John"));
c.createAlias("cars","crs");
c.add(Expression.eq("crs.make","Honda"));//one of John's cars is red Honda

And I set the color filter like this:
session.enableFilter("colorFilter").setParameter("color","black");

Now nothing comes back - no Owner objects are being returned at all. It's true that John has no black cars, but I would expect to get an Owner object back, with an empty collection of cars? After examining the SQL generated by Hibernate I saw that for some reason the filter condition was imported into the criteria SQL.
I'm guessing this is happening because criteria included alias to CAR table. What I don't understand is where does this dual approach come from? If Criteria is used to select the top-level object (Owner), and filters are used to control collections, then why are filters affecting the Criteria? Is this a bug of some sort?

I would appreciate it if someone could shed some light on this problem.
Thanks,

Bratek


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 28, 2006 6:16 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
No, that's the correct behaviour. To change the behaviour, you need to use an outer join. Do do this, you can either change your createAlias to createCriteria, or use setFetchMode(FetchMode.JOIN). (I think.. haven't tested that recently)

It's not a "dual approach". The query is asking for owners called John with black cars. It's not asking for owners called John, returning only black cars in their set. To change the question, you change the join mode.

_________________
Code tags are your friend. Know them and use them.


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