• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

Hibernate Criteria query for matching on fields of related objects?

 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In my model, I have a PhoneNumber class that breaks the number into 3 fields:

class PhoneNumber {
private Long id;
private String areaCode;
private String prefix;
private String number;
}

Then I have a Person class that has references to 3 phone numbers:

class Person {
private Long id;
private PhoneNumber cell;
private PhoneNumber home;
private PhoneNumber work;
}


These are mapped to two tables in the database, with cell, home and work columns in the Person table that are foreign keys into the PhoneNumber
table.

Now I need to allow users to find Person objects by entering the last 4 digits of the phone number. The query should match against the 'number' field of any of the Person's three phone numbers.

I'm having trouble coming up with a query that works. My first try was this:



This was supposed to set up an OR with each number, but of course it doesn't work because there are no fields named "cell.number", "home.number", or "work.number" in Person.

I then tried nested Criteria objects, something like this:

[CODE}
Criteria home = query.createCriteria("home");
home.add(Expression.like("number", "%" + searchString));
Criteria work = query.createCriteria("work");
work.add(Expression.like("number", "%" + searchString));
Criteria cell= query.createCriteria("cell");
cell.add(Expression.like("number", "%" + searchString));
[/CODE]

Now the query only returns Persons where all three numbers match the search strings. I can't see how to turn this approach into an OR.

Does anyone have any ideas how to approach this problem?

(It's too late in the project to change either the model or the database tables.)

Thanks in advance,


Steven Gollery
 
Steven Gollery
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I got it: two ways to do it. Either use a filter or combine the nested criteria using Expression.or. The filter is straightforward:



Steven
 
Every plan is a little cooler if you have a blimp. And a tiny ad.
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic