-->
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.  [ 10 posts ] 
Author Message
 Post subject: criteria across collections and OR
PostPosted: Fri Jun 25, 2004 6:45 pm 
Beginner
Beginner

Joined: Thu Sep 11, 2003 12:53 pm
Posts: 23
Hi,

I'm having trouble formulating a query with criteria. I'd like users to enter a search term, and have it check the term against User.first_name, User.last_name, and User.company.company_name. (company is a many-to-one property of User that returns a Company object).

This works without the company name:

Code:
c.add(
Expression.or (
    Expression.like( "first_name","%" + filter.getQuick_search() + "%"),
    Expression.like( "last_name","%" + filter.getQuick_search() + "%")
  )
);


but this gives an error.
Code:
c.add(
Expression.or (
  Expression.like( "first_name","%" + filter.getQuick_search() + "%"),
  Expression.or (
    Expression.like( "last_name","%" + filter.getQuick_search() + "%"),
    Expression.like( "company.company_name","%" + filter.getQuick_search() + "%") 
  )
)
);



Reading the docs, it looks like I need a "createCriteria" but I can't figure out where to put it.

Any suggestions? I'd like to use the Criteria object for querying since I have a filter with a number of properties that can all optionally be applied.

WILL


Top
 Profile  
 
 Post subject:
PostPosted: Sat Jun 26, 2004 9:54 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
Code:
c.add(
  Expression.or (
    Expression.like( "first_name","%" + filter.getQuick_search() + "%"),
    Expression.like( "last_name","%" + filter.getQuick_search() + "%")
  )
)
.createCriteria("company").add(
    Expression.like( "company_name","%" + filter.getQuick_search() + "%")
);


Top
 Profile  
 
 Post subject:
PostPosted: Sat Jun 26, 2004 4:54 pm 
Beginner
Beginner

Joined: Thu Sep 11, 2003 12:53 pm
Posts: 23
I tried that, it's not quite what I'm looking for. The expression you gave is the equivalent of an AND. It returns records where the company name matches AND either the first or last name match.

I'm looking for a 3 way OR. Either the first name OR the last name OR the company name could match. The idea is that the user enters a phrase e.g. "Smith" and it would match "John Smithson / IBM" or "Smith Jefferson / Nestle" or "John Doe / Smith and Sons".

WILL


Top
 Profile  
 
 Post subject:
PostPosted: Sat Jun 26, 2004 6:18 pm 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
Try this:

Code:
c.createAlias("company", "cp")
.add(
    Expression.or (
      Expression.like( "first_name","%" + filter.getQuick_search() + "%"),
      Expression.like( "last_name","%" + filter.getQuick_search() + "%"
      Expression.like( "cp.company_name","%" + filter.getQuick_search() + "%"
   )
  )
);


Top
 Profile  
 
 Post subject:
PostPosted: Sat Jun 26, 2004 6:54 pm 
Beginner
Beginner

Joined: Thu Sep 11, 2003 12:53 pm
Posts: 23
Works great! This might be a nice entry for the Advanced FAQ as I couldn't find a discussion of this anywhere. (I didn't really understand what createAlias meant)

For the record, the correct code is a little different. It is:

Code:
c.createAlias("company", "cp")
.add(
  Expression.or (
  Expression.like( "first_name","%" + filter.getQuick_search() + "%"),
  Expression.or (
    Expression.like( "last_name","%" + filter.getQuick_search() + "%"),
    Expression.like( "cp.company_name","%" + filter.getQuick_search() + "%")
    )
)
);


Top
 Profile  
 
 Post subject:
PostPosted: Sat Jun 26, 2004 7:04 pm 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
You could also do

Code:
c.createAlias("company", "cp")
.add(
  Expression.disjunction()
  .add(Expression.like( "first_name","%" + filter.getQuick_search() + "%"))
  .add(Expression.like( "last_name","%" + filter.getQuick_search() + "%"))
  .add(Expression.like( "cp.company_name","%" + filter.getQuick_search() + "%"))
);


Top
 Profile  
 
 Post subject:
PostPosted: Sat Jun 26, 2004 7:08 pm 
Beginner
Beginner

Joined: Thu Sep 11, 2003 12:53 pm
Posts: 23
definitely a little easier to understand, thanks.


Top
 Profile  
 
 Post subject:
PostPosted: Sun Jun 27, 2004 2:18 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
Also, instead of the string concatenation, use: Expression.like("foo", foo, MatchMode.ANYWHERE)


Top
 Profile  
 
 Post subject:
PostPosted: Sun Jun 27, 2004 3:06 am 
Beginner
Beginner

Joined: Thu Sep 11, 2003 12:53 pm
Posts: 23
Thanks for the tips -- very helpful.

One more question. I tried this, and the problem now is that any matched record is required to have a company object joined. But some of the User objects have a null for the company property. For example, there might be a User with a matching first_name but no company present. The example given will not match this record, since a valid company (matching or not) is required by the join

Is there any way to make the join an outer join instead of an inner join?

Thanks, WILL

P.S. As a side note (related to the recent blog entry), pfui to those who complain about the support for this project. It amazes me that such a popular project has great documentation and support. I've always had my questions answered (if sometimes a bit curtly). Look forward to purchasing the book.


Top
 Profile  
 
 Post subject:
PostPosted: Sun Jun 27, 2004 8:09 am 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
I don't think this is possible with the Criteria API yet - most likely you will have to use two queries.


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