当前位置: 代码迷 >> Web前端 >> Hibernate Criteria 多层次查询关联有关问题(转)
  详细解决方案

Hibernate Criteria 多层次查询关联有关问题(转)

热度:525   发布时间:2012-09-14 11:53:44.0
Hibernate Criteria 多层次查询关联问题(转)

弈蒌,收藏一下!

?

http://terrencemail.javaeye.com/blog/197821

hibernate 存在如下表及关联:

Java代码 复制代码
  1. class ?House{ ??
  2. ?? private ?string?houseid; ??
  3. ?? private ?string?housename; ??
  4. } ??
  5. class ?Shelf{ ??
  6. ?? private ?string?shelfid; ??
  7. ?? private ?House?house; ??
  8. } ??
  9. class ?Position{ ??
  10. ?? private ?string?positionid; ??
  11. ?? private ?Shelf?shelf; ??
  12. } ??
  13. class ?Warehouse{ ??
  14. ?? private ?string?warehouseid; ??
  15. ?? private ?Position?position; ??
  16. }??
class House{ private string houseid; private string housename; } class Shelf{ private string shelfid; private House house; } class Position{ private string positionid; private Shelf shelf; } class Warehouse{ private string warehouseid; private Position position; }


Warehouse外键关联Pisition, Position外键关联Shelf, Shelf外键关联houseid。
现在要对Warehouse使用Criteria查询, 查询House的id,可使用如下方法:

Java代码 复制代码
  1. Criteria?criteria?=?BaseUtil.getCriteria(Warehouse. class ); ??
  2. criteria.createAlias( "position" ,? "position" ); ??
  3. criteria.createAlias( "position.shelf" ,? "shelf" ); ??
  4. criteria.add(expression_r.eq( "shelf.house.houseid" ,? "111" ));??
Criteria criteria = BaseUtil.getCriteria(Warehouse.class); criteria.createAlias("position", "position"); criteria.createAlias("position.shelf", "shelf"); criteria.add(expression_r.eq("shelf.house.houseid", "111"));


如果要查询House的name, 必须再关联House表:

Java代码 复制代码
  1. Criteria?criteria?=?BaseUtil.getCriteria(Warehouse. class ); ??
  2. criteria.createAlias( "position" ,? "position" ); ??
  3. criteria.createAlias( "position.shelf" ,? "shelf" ); ??
  4. criteria.createAlias( "shelf.house" ,? "house" ); ??
  5. criteria.add(expression_r.eq( "house.housename" ,? "name?A" ));??
Criteria criteria = BaseUtil.getCriteria(Warehouse.class); criteria.createAlias("position", "position"); criteria.createAlias("position.shelf", "shelf"); criteria.createAlias("shelf.house", "house"); criteria.add(expression_r.eq("house.housename", "name A"));


以上代码就可以实现了。

可是还会出现一个问题:如果表路径position已经被关联过了(比如此criteria是传入的参数,已经关联了Alias路径position, 并查询过了),则会报错。解决方法:
检查criteria里面关联的路径,如果已经关联,则不再关联,直接使用就行了。

Java代码 复制代码
  1. class ?CriteriaUtil{ ??
  2. public ? static ?Criteria?addAlias(CriteriaImpl?criteriaImpl,?String?path,?String?name){ ??
  3. ?? if (path?==? null )? return ?criteriaImpl; ??
  4. ?? for (Iterator?iter?=?criteriaImpl.iterateSubcriteria();iter.hasNext();){ ??
  5. ????Subcriteria?subCriteria?=?(Subcriteria)iter.next(); ??
  6. ???? if (path.equals(subCriteria.getPath())) ??
  7. ?????? return ?criteriaImpl; ??
  8. ??} ??
  9. ?? return ?criteriaImpl.createAlias(path,?name); ??
  10. } ??
  11. }??
class CriteriaUtil{ public static Criteria addAlias(CriteriaImpl criteriaImpl, String path, String name){ if(path == null) return criteriaImpl; for(Iterator iter = criteriaImpl.iterateSubcriteria();iter.hasNext();){ Subcriteria subCriteria = (Subcriteria)iter.next(); if(path.equals(subCriteria.getPath()))
                  


(注:这段代码有时是存在问题的,不是所有的Criteria 都能转换为CriteriaImpl,可能会是Subcriteria。因此,需要确保传入的参数为CriteriaImpl)

调用代码就变成:

Java代码 复制代码
  1. Criteria?criteria?=?BaseUtil.getCriteria(Warehouse. class ); ??
  2. CriteriaUtil.addAlias((CriteriaImpl)criteria,? "position" ,? "position" ); ??
  3. CriteriaUtil.addAlias((CriteriaImpl)criteria,? "position.shelf" ,? "shelf" ); ??
  4. criteria.add(expression_r.eq( "shelf.house.houseid" ,? "111" ));?
  相关解决方案