我有一个非常复杂的标准,我用它来检索,排序和分页服务器端数据。我删除了下面的摘录:
// create criteria over a bunch of tables...
Criteria testCriteria = getSession().createCriteria(PriceRequest.class)
.setResultTransformer(CriteriaSpecification.DISTINCT_ROOT_ENTITY)
.setFetchMode("pricedBy", FetchMode.JOIN)
.setFetchMode("canceledBy", FetchMode.JOIN)
.setFetchMode("product", FetchMode.JOIN)
.setFetchMode("product.underlyings", FetchMode.JOIN)
.setFetchMode("product.tradedBy", FetchMode.JOIN)
.setFetchMode("product.requestedBy", FetchMode.JOIN)
.setFetchMode("fileUploads", FetchMode.JOIN);
// add various filter fields (only if required in real code)...
Criteria subCriteria = testCriteria
.createCriteria("product", JoinFragment.LEFT_OUTER_JOIN)
.setFetchMode("underlyings", FetchMode.JOIN)
.add(Restrictions.ge("maturityDate", new Date()));
testCriteria.addOrder(Order.desc("product.id")); // (1)
testCriteria.addOrder(Order.desc("product.maturityDate")); // (2)
List list = testCriteria.list();
语句(1)运行正常,语句(2)以以下异常结束:
SEVERE:Servlet休息的Servlet.service()引发了异常 org.hibernate.QueryException:无法解析属性: product.maturityDate:com.my.model.PriceRequest
我选择“maturityDate”作为示例,但我遇到了除“product.id”之外的所有产品属性的问题。
我也尝试为产品添加别名(“prod”)。这允许按到期日期(“prod.maturityDate”)排序,但未能将限制添加到子标准。
我不知道出了什么问题......
答案 0 :(得分:2)
createCriteria(association)不会创建别名,但是你需要一个别名
此外,您无法过滤关联并急切获取它,因为您可能会过滤掉初始化它所需的关联实体,因此会忽略fetchmode。使用相关子查询
// create criteria over a bunch of tables...
Criteria testCriteria = getSession().createCriteria(PriceRequest.class)
.setFetchMode("pricedBy", FetchMode.JOIN)
.setFetchMode("canceledBy", FetchMode.JOIN)
.createAlias("product", "prod")
.setFetchMode("prod.underlyings", FetchMode.JOIN)
.setFetchMode("prod.tradedBy", FetchMode.JOIN)
.setFetchMode("prod.requestedBy", FetchMode.JOIN)
.setFetchMode("fileUploads", FetchMode.JOIN)
.addOrder(Order.desc("prod.maturityDate")); // (2)
// add various filter fields (only if required in real code)...
if (...)
{
Criteria subCriteria = getSession().createCriteria(PriceRequest.class)
.createCriteria("product", JoinFragment.LEFT_OUTER_JOIN)
.add(Restrictions.ge("maturityDate", new Date()));
.setResultTransformer(Projection.id)
testCriteria = testCriteria
.add(Subqueries.PropertyIn("id", subCriteria));
.setFetchMode("underlyings", FetchMode.JOIN)
}
List list = testCriteria.list();