Parent表中有一个外键关联时, 需要查询外键对就表中的数据:
Criteria criteria = this.getCriteria(Parent.class);
//连接关联子对象child,且指定了连接方式为左外连接
criteria.createAlias("children", "c", CriteriaSpecification.LEFT_JOIN));
criteria.add(Expression.eq("c.name", "wander"));
List list = criteria.list();
但list里面并不是Parent类型的.而是Object[]的类型, 存放一个parent类型和一个children类型, 因此关联查询和非关联查询应该分开, 或进行一下结合:
方法一:
criteria.createAlias("children", "c", CriteriaSpecification.LEFT_JOIN));
criteria.add(Expression.eq("c.name", "wander"));
List list = criteria.returnMaps().list();
Iterator iter = cats.iterator();
while ( iter.hasNext() ) {
Map map = (Map) iter.next();
Parent parent= (Parent) map.get(Criteria.ROOT_ALIAS);
Children c= (Children) map.get("c");
}
方法二:
criteria.createAlias("children", "c", CriteriaSpecification.LEFT_JOIN));
criteria.add(Expression.eq("c.name", "wander"));
List list = criteria.returnMaps().list();
List listTemp = new ArrayList(list.size());
for (Object obj : listTemp) {
if (obj instanceof Object[]) {
for (Object innerObj : (Object[])obj) {
if (criteria instanceof CriteriaImpl &&
innerObj.getClass().getName().equals(((CriteriaImpl)criteria).getEntityOrClassName())) {
list.add(innerObj);
break;
}
}
} else {
list = listTemp;
break;
}
}
Criteria criteria = this.getCriteria(Parent.class);
//连接关联子对象child,且指定了连接方式为左外连接
criteria.createAlias("children", "c", CriteriaSpecification.LEFT_JOIN));
criteria.add(Expression.eq("c.name", "wander"));
List list = criteria.list();
但list里面并不是Parent类型的.而是Object[]的类型, 存放一个parent类型和一个children类型, 因此关联查询和非关联查询应该分开, 或进行一下结合:
方法一:
criteria.createAlias("children", "c", CriteriaSpecification.LEFT_JOIN));
criteria.add(Expression.eq("c.name", "wander"));
List list = criteria.returnMaps().list();
Iterator iter = cats.iterator();
while ( iter.hasNext() ) {
Map map = (Map) iter.next();
Parent parent= (Parent) map.get(Criteria.ROOT_ALIAS);
Children c= (Children) map.get("c");
}
方法二:
criteria.createAlias("children", "c", CriteriaSpecification.LEFT_JOIN));
criteria.add(Expression.eq("c.name", "wander"));
List list = criteria.returnMaps().list();
List listTemp = new ArrayList(list.size());
for (Object obj : listTemp) {
if (obj instanceof Object[]) {
for (Object innerObj : (Object[])obj) {
if (criteria instanceof CriteriaImpl &&
innerObj.getClass().getName().equals(((CriteriaImpl)criteria).getEntityOrClassName())) {
list.add(innerObj);
break;
}
}
} else {
list = listTemp;
break;
}
}