-->
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: Query Problem: Criteria, collections, filters.
PostPosted: Thu Oct 18, 2007 6:42 am 
Newbie

Joined: Tue Oct 09, 2007 4:48 am
Posts: 11
Hi,

I'll try posting to the correct forum this time...

Having some blues trying to sort out how best to perform a particular query.

- I have a 'personcontainer' which contains a list of 'person'
- Each person has an 'organisation'

Diagram of the situation (no effort spared!):
Image
(from: http://www.flickr.com/photos/15331868@N02/1612476509/)

I need to find:
- a list of people **in a particular container** that match certain criteria in any of the main attributes of the organisation or the person.

Getting people in a particular container is fine:
Code:
session.createFilter(personcontainer.getPeople(), "")
   .setFirstResult(startRecord)
   .setMaxResults(itemsPerPage)
   .list();


Getting ALL people matching certain criteria in any of the main organisation or person attributes is also fine:
Code:
Criteria criteria = session.createCriteria(Person.class);
criteria.createAlias("organisation", "o");

Disjunction disjunction = Restrictions.disjunction();

Criterion firstNameCrit = Restrictions.ilike("firstName", searchTerm, MatchMode.ANYWHERE);
Criterion lastNameCrit = Restrictions.ilike("lastName", searchTerm, MatchMode.ANYWHERE);
Criterion orgNameCrit = Restrictions.ilike("o.name", searchTerm, MatchMode.ANYWHERE);
Criterion orgTelCrit = Restrictions.ilike("o.telephone", searchTerm, MatchMode.ANYWHERE);

disjunction.add(firstNameCrit);
disjunction.add(lastNameCrit);
disjunction.add(orgNameCrit);
disjunction.add(orgTelCrit);               

criteria.add(disjunction);

criteria.list();


BUT HOW??? do I get people matching the criteria JUST contained in the container?

I really appreciate any help you can give.

James.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 18, 2007 11:24 am 
Newbie

Joined: Tue Oct 09, 2007 4:48 am
Posts: 11
I've come up with a solution to this, but it's not pretty, and is going to be seriously inefficient when there are lots of records.

If anyone has a better way to do this (or even one that is slightly less inefficient - I am pulling in the whole collection before I even do anything), I'd be grateful for your thoughts.

Code:
//get all people in this personcontainer
List people = session.createFilter(personcontainer.getPeople(), "").list();

Long[] peopleIds = new Long[people.size()];

//find id's of the people in this container.
for(int i=0;i<people.size();i++){
   Person p = (Person) people.get(i);
   peopleIds[i]=p.getIdentifier();
}

//create new criteria as before, but add an 'in' Restriction...
Criteria criteria = session.createCriteria(Person.class);
criteria.createAlias("organisation", "o");
criteria.add(Restrictions.in("identifier", peopleIds));

//carry on as before...
Disjunction disjunction = Restrictions.disjunction();

Criterion firstNameCrit = Restrictions.ilike("firstName", psTerm, MatchMode.ANYWHERE);
//.....


Many thanks,

James.


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.