I have ManyToOne relationship which one table linking multiple entity.
Example Shop (Animal Shop) which only sell Cat and Dog:
Shop-shopId
-shopCode
Cat-catId ---------catId
-catName
-catType
Dog-dogId----------dogId
-dogName
-dogType
Linking relationships Are
1. Shop ManyToOne Cat
2. Shop ManyToOne Dog
Below Is the Code for Searching with filtering criteria.
Code:
public List<Shop>searchByShop (QueryRequest<Shop> query){
Shop example = query.getExample();
DetachedCriteria criteria = DetachedCriteria.forClass(Shop.class);
//Filtering
// Check not null or empty
criteria.add(Restrictions.eq("shopCode", example.getShopCode()));
// Check not null or empty
criteria.createCriteria("cat")
.add(Restrictions.ilike("catName", example.getCat().getCatName());
.add(Restrictions.ilike("catType", example.getCat().getCatType());
// Check not null or empty
criteria.createCriteria("dog")
.add(Restrictions.ilike("dogName", example.getDog().getDogName());
.add(Restrictions.ilike("dogType", example.getDog().getDogType());
return getHibernateTemplate().findByCriteria(criteria);
}
The Problem above code is this line:
Quote:
//Included from search
// Check not null or empty
criteria.createCriteria("cat")
.add(Restrictions.ilike("catName", example.getCat().getCatName());
.add(Restrictions.ilike("catType", example.getCat().getCatType());
//Excluded from search
// Check not null or empty
criteria.createCriteria("dog")
.add(Restrictions.ilike("dogName", example.getDog().getDogName());
.add(Restrictions.ilike("dogType", example.getDog().getDogType());
When you enter catName and catType for search ,it works accordingly. [ First come first server ]
When you enter catName , catType, dogName and dogType for search, it will output everything.( without filter)
May I know how to solve this problem?
Which library or method I should call appropriately?
Thanks