-->
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.  [ 4 posts ] 
Author Message
 Post subject: Creating Criteria
PostPosted: Tue May 31, 2005 12:31 pm 
Beginner
Beginner

Joined: Fri Jun 04, 2004 12:50 pm
Posts: 32
ok, I am sure this is not as hard as what I am making it out to be. This is the problem.

I have a list of criteria that I need to add to a Criteria object. But, the criteria comes from different entities and we therefore need to use the following "code fragment" example.

List cats = sess.createCriteria(Cat.class)
.add( Restrictions.like("name", "F%")
.createCriteria("kittens")
.add( Restrictions.like("name", "F%")
.list();

Thats great. I understand all of this. BUT, how do we code this example to be more general. The user can tell me how many criteria to search by. How do you code it so that we can have "x" number of "Criteria.add" methods but with multiple associations. Following on from the example above I could have

List cats = sess.createCriteria(Cat.class)
.add( Restrictions.like("name", "F%")
.createCriteria("kittens")
.add( Restrictions.like("name", "F%")
.add( Restrictions.eq("age", "%")
.add( Restrictions.eq("x", "%")
.add( Restrictions.eq("y", "%")
.list();

How do you generalize this code it so that we can add any number of multiple restrictions? I hope this makes sense to people out there.

Cheers
Tom

Hibernate version:
Hibernate Version 3.0


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 31, 2005 2:20 pm 
Senior
Senior

Joined: Tue Feb 08, 2005 5:26 pm
Posts: 157
Location: Montréal, Québec - Canada
Maybe you could use Query by example.

You can create an empty Cat object and insert the values that you would like to discriminate on: Ex;

Let's say you want to search for all cats named Teddy, you could do:

Code:
Cat searchCat = new Cat();
cat.setName("Teddy");
Collection cats = session.createCriteria(Cat.class).addCriteria(Example.add(searchCat)).list();


(I am not sure if this code compiles, but it should be pretty close :)

You need to validate if you can do the following:
Code:
Cat searchCat = new Cat();
Cat kittenCat = new Cat();
cat.setName("Teddy");
kittenCat.setName("Donald");
cat.addKitten(kittenCat)
session.createCriteria(Cat.class).addCriteria(Example.add(searchCat)).list();


I am not sure if you can specify criterias in associated classes. Let me know if it works as well.

This way, you would not have to manage how many criterias have been entered for the search query. It would automatically be handled by Hibernate.

If you can't use the Query by example, I think you will have to check for every possible search criteria and manage it if it is specified.

Good luck,
Vincent.

_________________
Vincent Giguère
J2EE Developer


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 31, 2005 3:03 pm 
Beginner
Beginner

Joined: Fri Jun 04, 2004 12:50 pm
Posts: 32
This is an interesting way of doing things.....but there must be a better way.

I have home rolled Search objects which hold all the information that the criteria will need. Having to create objects for the examples seems like quite a waste of resources and also adds another level of complexity on to the situation


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 31, 2005 4:39 pm 
Senior
Senior

Joined: Tue Feb 08, 2005 5:26 pm
Posts: 157
Location: Montréal, Québec - Canada
Well...

Unless you figure some funky algorithm using introspection, I don't see how you can automatically work around your problem.

The fact is that you home search attributes are unknown to hibernate. I don't see any other way than to go through all of your search attribute and add an extra criteria when needed.

Please let me know if you find a more elegant way. I would be interested in knowing your solution.

Good Luck

_________________
Vincent Giguère
J2EE Developer


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