The criteria api mostly does what I am looking for.
However, I believe there is a bug.
Give the function below
crit.add(Expression.eq("model.description", map.get("modelDesc")));
should be the same as
modelCriteria = crit.createCriteria("model");
modelCriteria.add(Expression.eq("description", map.get("modelDesc")));
However, the first method throws a QueryException ("could not resolve property") . According to page 218 of the Hibernate in action(sample chapter), I can dereference child objects with the . notation.
Code:
public List queryByCriteriaMap(Map map) throws ServiceException, HibernateException {
List list = null;
Criteria modelCriteria = null;
Criteria crit = this.getSession().createCriteria(Netelement.class);
if (map.containsKey("elementName")) {
crit.add(Expression.eq("name", map.get("elementName")));
}
if (map.containsKey("vendorName")) {
if (modelCriteria ==null) {
modelCriteria = crit.createCriteria("model");
}
modelCriteria.createCriteria("vendor").add(Expression.eq("name", map.get("vendorName")));
}
if (map.containsKey("modelDesc")) {
if (modelCriteria ==null) {
modelCriteria = crit.createCriteria("model");
}
//crit.add(Expression.eq("model.description", map.get("modelDesc")));
modelCriteria.add(Expression.eq("description", map.get("modelDesc")));
}
if (map.containsKey("type")) {
if (modelCriteria ==null) {
modelCriteria = crit.createCriteria("model");
}
modelCriteria.createCriteria("deviceType").add(Expression.eq("name", map.get("type")));
}
if (map.containsKey("ipaddr")) {
crit.add(Expression.eq("ipaddr", map.get("ipaddr")));
}
if (map.containsKey("globalsync")) {
crit.add(Expression.eq("globalsync", map.get("globalsync")));
}
if (map.containsKey("agentName")) {
Criteria agentCriteria = crit.createCriteria("Agents");
agentCriteria.add(Expression.eq("name", map.get("agentName")));
}
if (map.containsKey("containerName")) {
Criteria containerCriteria = crit.createCriteria("containers");
containerCriteria.add(Expression.eq("name", map.get("containerName")));
}
return crit.list();
}