-->
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.  [ 1 post ] 
Author Message
 Post subject: Criteria question
PostPosted: Sun Apr 20, 2008 8:20 pm 
Newbie

Joined: Sun Apr 20, 2008 7:18 pm
Posts: 3
Apologies for the cross post, but this one's relevant to all hibernate users as well as nhibernate so thought you might be able to help ...

Can anyone suggest any simplification for the following query? I'm looking for people who have an address in paris and an address in london.
Query is currently so complex to avoid searching for 'person with address where town = london and town = new york' which is obviously wrong*
By the way, if you come up with a response, any suggestions on where I could have researched the answer would be appreciated also!

Code:
   Junction addressJunction = new Conjunction();
   DetachedCriteria addressCriteria = DetachedCriteria.For<Person>()
      .SetProjection(Projections.Id())
      .CreateCriteria("Addresses")
      .Add(Restrictions.Like("Town", "paris"));
   addressJunction.Add(Subqueries.PropertyIn("ID", addressCriteria));

   addressCriteria = DetachedCriteria.For<Person>()
      .SetProjection(Projections.Id())
      .CreateCriteria("Addresses")
      .Add(Restrictions.Like("Town", "london"));
   addressJunction.Add(Subqueries.PropertyIn("ID", addressCriteria));

   DetachedCriteria rootPersonQuery = DetachedCriteria.For<Person>("person")
      .Add(addressJunction);

I'd ideally like a simple query that looks something like (pseudo-nhibernate)

Code:
   Criteria = DetachedCriteria.For<Person>("person")
      .ConunctionOf()
      .CreateCriteria("Addresses").Add(Restrictions.Like("Town", "london"))
      .CreateCriteria("Addresses").Add(Restrictions.Like("Town", "paris"))

Get my drift?

* Note: this returns the wrong 'person with address where town = london and town = new york'

Code:
   Junction addressJunction = new Conjunction();
   DetachedCriteria addressCriteria = DetachedCriteria.For<Address>("address")
      .Add(Restrictions.Like("Town", "paris"))
      .SetProjection(Projections.Id());
   addressJunction.Add(Subqueries.PropertyIn("ID", addressCriteria));

   addressCriteria = DetachedCriteria.For<Address>("address")
      .Add(Restrictions.Like("Town", "london"))
      .SetProjection(Projections.Id());
   addressJunction.Add(Subqueries.PropertyIn("ID", addressCriteria));

   DetachedCriteria rootPersonQuery = DetachedCriteria.For<Person>("person")
      .CreateCriteria("Addresses")
      .Add(addressJunction)
      .SetProjection(Projections.Property("Surname"));


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

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.