-->
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.  [ 8 posts ] 
Author Message
 Post subject: Criteria Search
PostPosted: Wed Oct 11, 2006 1:41 pm 
Newbie

Joined: Mon Nov 28, 2005 11:15 am
Posts: 13
Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp

Hibernate version:
3.0

I am trying to build a query to search for a text string in any of the following: Car.name or Car.description or parts name or parts.description.

Code:
class Car {
    private String name;
    private String description;
    private Set parts;
}

class Part {
    private String name;
}

List cats = sess.createCriteria(Car.class)
    .add( Restrictions.or(Restrictions.like("name", "F%") ),
               Restrictions.like("description", "F%")))
    .createCriteria("parts")
        .add( Restrictions.like("name", "F%") )
    .list();


The above however does not do an "OR" on the parts name but seems to be an "AND" for the child table. How can get all the Cars whose name contains F% or whose description contains F% or whose parts contains F% in the name of the part?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Oct 11, 2006 1:51 pm 
Beginner
Beginner

Joined: Tue Aug 08, 2006 11:53 am
Posts: 37
Use Disjunction class e.g.

List cats = sess.createCriteria(Car.class) ;
Disjunction disjunction = Restrictions.disjunction();
disjunction.add(Restrictions.like("description", "F%"));
...


Top
 Profile  
 
 Post subject:
PostPosted: Wed Oct 11, 2006 1:57 pm 
Newbie

Joined: Mon Nov 28, 2005 11:15 am
Posts: 13
Could you please expand on this?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Oct 11, 2006 3:30 pm 
Newbie

Joined: Mon Jul 26, 2004 11:35 am
Posts: 13
There are two useful classes Conjunction -> value="a" and value = "b"
and Disjunction -> value="a" or value="b"

To Create either: (your case)
Disjunction disjunction = Restrictions.disjunction();

String[] properties = new String[]{"name1","name2","name3"};
for(int i = 0; i < properties.length; i++)
disjunction.add(Restrictions.ilike(properties[i],"F%");

criteria.add(disjunction);

That would solve your issue. You can take the code above and create generic helper method:

public static final void disjunction(Criteria c,String[] properties;String value) {
Disjunction disjunction = Restrictions.disjunction();
for(int i = 0; i < properties.length; i++)
disjunction.add(Restrictions.ilike(properties[i],"F%");
c.add(disjunction);
}

I prefer this approach since I hate code duplication
Good luck
Roman


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 12, 2006 11:38 am 
Newbie

Joined: Mon Nov 28, 2005 11:15 am
Posts: 13
Thanks for the responses. I see the use of the disjunction but am not being able to follow as to how it can be used for the child parts without creating a second criteria.

Code:
Disjunction d = Restrictions.disjunction();
d.add(Restrictions.like("description","F%"));
d.add(Restrictions.like("name", "F%"));
[b] d.add(Restrictions.like("parts.name", "F%"));   [/b]


The code in bold will not work. I am missing something wherein I need to add a criteria for parts.

In short, the query:
select * from car c where c.name like 'F%' or c.description like 'F%' or c.id in (select p.carId from parts p where p.description like 'F%');

is what I am trying to build.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 12, 2006 11:54 am 
Newbie

Joined: Mon Jul 26, 2004 11:35 am
Posts: 13
What you need is to create an alias:

criteria.createAlias("parts","p");

then use:

disjunction.add(Restrictions.like("p.name","F%"));

That should do the trick


P.S. Please rate my answers
Thanks
Roman


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 12, 2006 12:07 pm 
Newbie

Joined: Mon Nov 28, 2005 11:15 am
Posts: 13
I figured out another way to do this:

Code:
Disjunction d = Restrictions.disjunction();
d.add(Restrictions.like("name", "F%"));
d.add(Restrictions.like("description", "F%"));
      
DetachedCriteria detachedCriteria = DetachedCriteria.forClass(Part.class);
// Project prorty of Part, car.id
detachedCriteria .setProjection(Projections.property("car.id"));
detachedCriteria.add(Restrictions.like("name", "F%"));      

d.add(Property.forName("id").in(detachedCriteria ));

hibernateQuery.add(d);
List results = hibernateQuery.list();


The query you mentioned earlier Roman provides duplicate results. The above seems to give me the correct one.

PS: How do I rate you answer or give you points?


Top
 Profile  
 
 Post subject: Workaround for the DetachedCriteria/createAlias problem?
PostPosted: Wed Oct 18, 2006 5:30 am 
Beginner
Beginner

Joined: Wed Oct 18, 2006 4:41 am
Posts: 20
Location: West Yorkshire, UK
Hi, I have looked into using this 'IN' query example but have come across what seems to be a known issue with hibernate DetachedCriteria - the createAlias does not create the required join when using subcriteria (Jira issue 1685).

I am developing quite a complex 'search' facility and as such would like to use the Criteria objects to build up my query, rather than needing to delve into HQL. Could anybody please explain a suitable (simple!) workaround to achieve this, as I am fairly new to the ORM/hibernate world?

Any help would be much appreciated!

Thanks, Carl


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