-->
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.  [ 1 post ] 
Author Message
 Post subject: problem in Criteria query on polymorphic one-to-many
PostPosted: Wed Jul 30, 2008 3:42 am 
Newbie

Joined: Wed Jul 30, 2008 3:37 am
Posts: 1
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!


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

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.