Hi Can anyone help me create a criteria base on the following information.
Basically this code will be added to a function that returns the number of found rows with
the number of child rows greater than 10. The value 10 will be dynamic.
I have been stuck with this problem for a day already and need some help.
Note that I can not control the database itself so I can not use views or change the database schema. Also this is a simplified version of the problem other properties of client and mapfields are omitted and the creation of the projections and criteria will be refactored to a generic helper method.
public class client
{
private int id;
private string code;
@OneToMany(fetch=FetchType.Lazy)
@JoinColumn(name="code", nullable=true)
private List <MapFields> mapFields;
}
public class MapFields
{
private int id;
private string code;
}
How can I do this sql query using hibernate criteria.
SELECT COUNT(*)
FROM
(
SELECT MapFields.code
FROM Client LEFT JOIN MAPFields on Client.code
GROUP BY MapFields.code
HAVING COUNT(MapField.code) > 10
) AS A
I have tried the following but to no avail. I keep encountering an exception that tell me that a property can not be resolved.
org.hibernate.QueryException: could not resolve property: numberOfMapFields of: …Client
Criteria criteria = getCurrentSession().createCriteria(Client.class)
criteria.createAlias("mapFields", "mapFields", CriteriaSpecification.Left_Join)
ProjectionList pl = Projections.projectionList();
pl.add(Projections.count("mapFields.code", "numberOfMapFields"));
pl.add(Projections.rowCount());
criteria.setProjection(projectionList);
criteria.add(Restriction.ge("numberOfMapFields", "10"))
criteria.list();
PLEASE can anyone provide a code snippet on how to do this using hibernate criteria and if you need more information just tell me and i will try to provide more information.
Doh! Thanks for the information…