Hi everybody,
I'm having a little trouble using Criteria's for the following situation
Class Product
- Property title [String]
- Property descriptions [List<ProductDescription>]
Class ProductDescription
- Property title
Now i'd like to do something like (in pseudo SQL)
Code:
SELECT FROM Product
WHERE title LIKE '%foo%'
OR descriptions.title LIKE '%foo%'
OR descriptions EMPTY
which means fetch all Product instances, where either their title or one of the descriptions title matches a given String - with the special case, that there might be no descriptions existing.
First I tried this
Code:
Criteria criteria = session.createCriteria(Product.class);
criteria.add(Restrictions.like("title", "foo", MatchMode.ANYWHERE);
Criteria descC = criteria.createCriteria("descriptions");
descC.add(Restrictions.like("title", "foo",MatchMode.ANYWHERE);
List<?> result = criteria.list();
Which results in an AND conjunction, instead of an OR conjunction. The next thing I tried was:
Code:
Criteria criteria = session.createCriteria(Product.class);
criteria.createAlias("descriptions", "desc");
criteria.add(Restrictions.or(Restrictions.like("desc.title", titleField.getText(), MatchMode.ANYWHERE),
Restrictions.like("title", titleField.getText(), MatchMode.ANYWHERE)
));
This is better, but only Product objects which actually do have at least one ProductDescription are being fetched. Any product which doesn't have any ProductDescriptoins associated is ignored.
Is there any way to also include these records into the Criteria result?
---
Hibernate version:
3.0
Name and version of the database you are using:
MySQL 5