Hibernate的Criteria查询问题。

请问Criteria支持嵌套属性吗?比如:
Criteria criteria = session.createCriteria(Account.class);
criteria.add(Expression.eq("accountProfile.homeAddress.province",address.getProvince()));

其中Account类中有AccountProfile属性,AccountProfile有homeAddress属性,Address有province属性。

嵌套问题你首先要从关联关系方面去考虑,你是否配置1:N这样关系,懒加载如何 ,等等。

accountProfile.homeAddress.province写法是HQL方式,是对象概念在过程平台中的变相表达方式,类似OGNL
Criteria是对象方式查询,可使用setFetchMode FetchMode.JOIN方式

http://www.devarticles.com/c/a/Java/Hibernate-Criteria-Queries-in-Depth/

我记得好像有个版本的hibernate只支持a.b,不支持第二级的a.b.c。
用join关联载入就可以了,这是一定能取到完整聚合的、没有延迟载入的方式。

多谢了。但是遇到如下问题。
Criteria criteria = session.createCriteria(Account.class);
criteria.setFetchMode("accountProfile",FetchMode.JOIN);
criteria.add(Restrictions.eq("homeAddress.province",address.getProvince()));

org.springframework.orm.hibernate3.HibernateQueryException: could not resolve property: homeAddress of: com.sns.xmu.model.Account; nested exception is org.hibernate.QueryException: could not resolve property: homeAddress of: com.sns.xmu.model.Account

Account与AccountProfile是一对一的关联。