[hibernate-issues] [Hibernate-JIRA] Created: (HHH-3408) problem in hibernate Criteria query on a polymorphic one-to-many relation

Auni (JIRA) noreply at atlassian.com
Wed Jul 30 04:02:47 EDT 2008


problem in hibernate Criteria query on a polymorphic one-to-many relation
-------------------------------------------------------------------------

                 Key: HHH-3408
                 URL: http://opensource.atlassian.com/projects/hibernate/browse/HHH-3408
             Project: Hibernate3
          Issue Type: Bug
          Components: query-criteria
    Affects Versions: 3.2.5
            Reporter: Auni


Im working on an hibernate-3 based application with following POJOs as domain model:

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 using both lines 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?


-- 
This message is automatically generated by JIRA.
-
If you think it was sent incorrectly contact one of the administrators: http://opensource.atlassian.com/projects/hibernate/secure/Administrators.jspa
-
For more information on JIRA, see: http://www.atlassian.com/software/jira

        



More information about the hibernate-issues mailing list