使用标准 API 订购

2022-08-31 13:18:56

当我编写 HQL 查询时

Query q = session.createQuery("SELECT cat from Cat as cat ORDER BY cat.mother.kind.value");
return q.list();

一切都很好。但是,当我编写标准时

Criteria c = session.createCriteria(Cat.class);
c.addOrder(Order.asc("mother.kind.value"));
return c.list();

我遇到异常org.hibernate.QueryException: could not resolve property: kind.value of: my.sample.data.entities.Cat

如果我想使用标准和订单,我应该如何表达我的“订单依据”?


答案 1

您需要为 创建别名。你这样做是这样的。mother.kind

Criteria c = session.createCriteria(Cat.class);
c.createAlias("mother.kind", "motherKind");
c.addOrder(Order.asc("motherKind.value"));
return c.list();

答案 2

如果没有看到映射,很难确定(请参阅@Juha的评论),但我认为您想要如下所示的内容:

Criteria c = session.createCriteria(Cat.class);
Criteria c2 = c.createCriteria("mother");
Criteria c3 = c2.createCriteria("kind");
c3.addOrder(Order.asc("value"));
return c.list();

推荐