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.  [ 3 posts ] 
Author Message
 Post subject: Search for parent object by two child criteria
PostPosted: Fri Aug 24, 2007 10:42 am 
Newbie

Joined: Fri Aug 24, 2007 10:11 am
Posts: 5
Hibernate version: 1.2.0.3001

Name and version of the database you are using: 10g Enterprise Edition Release 10.2.0.1.0


Hi,

I have class Parent with one to many Child collection.

class Child
{
private string key;
private string value;
}

class Parent
{
private IList<Child > children
}

I want to search for Parent objects that has two Child elements as one of them has key="key1" and value="value1" and the other one has key="key2" and value="value2".

It is easy to implement this as OR operation

ICriteria criteria = CreateCriteria();
ICriteria criteriaChild = criteria.CreateCriteria("Children");

criteriaChild.Add(
Expression.Or(
Expression.And(
Expression.Eq("key", "key1"),
Expression.Eq("value", "value1")),
Expression.And(
Expression.Eq("key", "key2"),
Expression.Eq("value", "value2")));

but I can not find a way to implement it as AND operation.

Any help will be appresiated.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Aug 24, 2007 2:21 pm 
Expert
Expert

Joined: Fri May 13, 2005 11:13 am
Posts: 292
Location: Rochester, NY
Even in SQL, the latter is far more difficult than the former. Given the 'or':
Code:
select p.* from Parents p join Children c on (c.ParentID = p.ID)
where (c.key = 'key1' and c.value = 'value1') or (c.key = 'key2' and c.value = 'value2')

one can see that just replacing the 'or' with an 'and' will always result an empty relation: any given Child can only have one or the other key-value. So, you have to join Children twice, or use an 'in' which I think would be far more efficient:
Code:
select p.* from Parents p
where p.ID in ( select ParentID from Children c where (c.key = 'key1' and c.value = 'value1') )
   and p.ID in ( select ParentID from Children c where (c.key = 'key2' and c.value = 'value2') )


as criteria:

Code:
ICriteria criteria = session.CreateCriteria( typeof( Parent ) );
DetachedCriteria child1 = (new DetachedCriteria( typeof( Child ) ).Add( Expression.And( Expression.Eq( "key", "key1" ), Expression.Eq( "value", "value1" ) ) ).Add( Projections.Property( "ParentID" ) );
DetachedCriteria child2 = (new DetachedCriteria( typeof( Child ) ).Add( Expression.And( Expression.Eq( "key", "key2" ), Expression.Eq( "value", "value2" ) ) ).Add( Projections.Property( "ParentID" ) );
criteria.Add( Expression.And( Subqueries.PropertyIn( "ID", child1), Subqueries.PropertyIn( "ID", child2 ) ) );


Getting the ParentID off the children as a property projection might be more complicated than that, like
Code:
.CreateAlias( "Parent", "p" ).Add( Projections.Property( "p.ID" ) );

but there's something to get you started.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 18, 2007 4:55 pm 
Newbie

Joined: Thu Jun 14, 2007 3:46 am
Posts: 17
wrong thread posted


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