-->
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: Criteria API - createCriteria
PostPosted: Wed Apr 25, 2007 6:15 am 
Newbie

Joined: Wed Apr 25, 2007 5:46 am
Posts: 7
Hi all,

I am facing a different problem now. The fields in my search operation are not mandatory. The user will enter the sort criteria and the order of sorting.
In the below code companyName and companyType belong to Company POJO and city and countryCode belong to CompanyAddress POJO. Now Search is successful only when I choose atleast one values from both of the POJO and fails otherwise. The sort order is also entered by the user. How can I decide the sort order based on the users choice? The search has to be made even with a value from a single POJO?


SessionFactory sessionFactory = new Configuration().configure()
.buildSessionFactory();
session = sessionFactory.openSession();

List crit = session.createCriteria(Company.class).add(Restrictions.or(Restrictions.like("searchName",compname),Restrictions.like("companyType", companyType)))
.addOrder(Order.asc(sortCriteria))
.createCriteria("addresses", "address")
.add(Restrictions.or(Restrictions.like("address.city",city),Restrictions.like("address.countryCode",ctry )))
.list();
session.close();
System.out.println("Results----->"+crit.size());
return crit;


Where I am wrong here?. How could we change the default and operation to an or operation?

With Regards,
Amutha[/code]


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 25, 2007 6:43 am 
Newbie

Joined: Thu Apr 19, 2007 1:46 am
Posts: 10
Well in this case you cannot hardcode it in the first place beacuse you never know what the user may want to filter on . I had a similar case which i coded in the following way . This code may help u out.
It may not run this is just for an explantion.

public List findByCriteria(List<Criterion> criterion, List<Criterion> innerCriteria) {

// criterion contains the list of all restrictions

Criteria crit = currentSession().createCriteria(Company.class);
Criteria crit2 = null;

for (Criterion c : criterion) {
crit.add(c);

}




for (Criterion c : inner) {
crit2.add(c);
}


crit = crit2;

}


return crit.list();
}


Top
 Profile  
 
 Post subject: Filtering using Query By Criteria
PostPosted: Wed Apr 25, 2007 6:54 am 
Newbie

Joined: Thu Apr 19, 2007 1:46 am
Posts: 10
Well in this case you cannot hardcode it in the first place beacuse you never know what the user may want to filter on . I had a similar case which i coded in the following way . This code may help u out.
It may not run this is just for an explantion.

public List findByCriteria(List<Criterion> criterion, List<Criterion> innerCriteria) {

// criterion contains the list of all restrictions

Criteria crit = currentSession().createCriteria(Company.class);
Criteria crit2 = null;

for (Criterion c : criterion) {
crit.add(c);

}


if (innerCriteria != null)
{

crit2 = crit.createCriteria("address");
for (Criterion c : inner) {
crit2.add(c);
}

crit = crit2;


}


return crit.list();
}


Similarly u may accept the list on which u would like to order and add it to the criteria .


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 25, 2007 7:01 am 
Newbie

Joined: Wed Apr 25, 2007 5:46 am
Posts: 7
Hi gurpreet,

Thanks for your immediate response. We are not sure about the users filter options. My criteria fields are not mandatory. The user may select any options.

Why do we require the List to be passed to the below method


public List findByCriteria(List<Criterion> criterion, List<Criterion> innerCriteria) {
}

In my case I have Company Pojo associated with CompanyAddress. Any idea to go with this?


With thanks,
Amutha[/code]


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.