[hibernate-users] Find with Criteria/Example: object containing a List

Lev dev at plektos.com
Thu Jul 29 20:19:20 EDT 2010


hi,

i am trying to execute a find using Criteria/Example on an
Entity that contains a List of enum. consider a class such
as:

@Entity
public class MockEntity extends ModelBase implements Serializable {

    @CollectionOfElements()
    @Enumerated(EnumType.STRING)
    private List<MockEnum> mockEnums;
    ...
}

here, the enum is defined as:

    public enum MockEnum {
        GOOD, BAD, OTHER;
    }

i am trying to find MockEntity objects containing a prescribed MockEnum
contained in the MockEntity.List<MockEnum> list.

i have created a findByExample() method that utilizes Criteria/Example
to execute the query:

    public List<T> findByExample(T exampleInstance, String[] excludeProperty) {
        Criteria criteria = getSession().createCriteria(entityClass);
        Example example = Example.create(exampleInstance);
        if (excludeProperty != null) {
            for (int i = 0; i < excludeProperty.length; i++)
                example.excludeProperty(excludeProperty[i]);
        }
        criteria.add(example);
        criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
        return criteria.list();
    }

i tried calling the findByExample() method as follows:

        MockEntity example = new MockEntity();
        List<MockEntity.MockEnum> enums = new ArrayList<MockEntity.MockEnum>();
        enums.add(MockEntity.MockEnum.OTHER);
        example.setMockEnum(enums);
        dao = new MockEntityDao();
        List<MockEntity> found = dao.findByExample(example, null);

but, the List called found contains all of the MockEntity residing in the
DB -- not merely the MockEntity containing MockEnum.OTHER in
the associated MockEntity.List<MockEnum>.

does anybody have suggestions on how i can achieve this: find
MockEntity objects that contain a prescribed MockEnum in their list?

thank you for your help.


More information about the hibernate-users mailing list