Im working on a hibernate-3 based application with following POJOs as domain model:
@Entity
@Table
User{
Long id;
String username;
String password;
List<Baseinfoset> infosets;
...
@OneToMany(cascade = CascadeType.ALL)
public List<BaseInfoset> getInfosets() {
return infosets;
}
...
}
@Entity
@Table
@Inheritance(strategy=InheritanceType.JOINED)
BaseInfoset{
Long id;
...
}
@Entity
@Table
PersonalInfo extends BaseInfoset{
String firstName;
String lastName;
...
}
@Entity
@Table
AddressInfo extends BaseInfoset{
String street;
String city;
String country;
...
}
The idea is User object contains a set field named 'infosets' which is a collection(set) like {PersonalInfo, AddressInfo} of which each element is of type BaseInfoset.
I can add/persist User information fine along with PersonalInfo and AddressInfo, so i think the association mapping is ok.
My problem is to implementing a search method on User using Hibernate Criteria query....
Here is what i have in my search method:
for example: i would like to find User with a particular "firstName" and from particular "country"
//case1: using alias
1. Criteria critUser = session.createCriteria(User.class);
2. critUser.createAlias("infosets", "infosetList", Criteria.LEFT_JOIN);
//match field from PersonalInfo object
3. critUser.add(Restrictions.ilike("infosetList.firstName", firstName, MatchMode.ANYWHERE));
//match field from AddressInfo object
4. critUser.add(Restrictions.ilike("infosetList.country", country, MatchMode.ANYWHERE));
5. critUser.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY).list();
//case2: using new criteria
1. Criteria critUser = session.createCriteria(User.class);
2. Criteria critInfoset= critUser.createCriteria("infosets", Criteria.LEFT_JOIN);
//match field from PersonalInfo object
3. critInfoset.add(Restrictions.ilike("firstName", firstName, MatchMode.ANYWHERE));
//match field from AddressInfo object
4. critInfoset.add(Restrictions.ilike("country", country), MatchMode.ANYWHERE);
5. critUser.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY).list();
Now the problem in both cases is, the criteria returns result only for either line 3 or line 4 individually, but not for both at a time. I mean if i use only line3 or line4 then it returns result for both cases but if i use both lines at a time it returns empty result although the restrictions match for both fields.
Seems it works for either adding criteria on PersonalInfo object's field or AddressInfo object's field but not on both at a time in which case it returns empty result.
Is it a limitation of Hibernate criteria query or i am missing something here?
Please help!
|