当我编写 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
如果我想使用 Criteria and Order,我应该如何表达我的“order by”?
最佳答案
您需要为 mother.kind
创建一个别名。你这样做是这样的。
Criteria c = session.createCriteria(Cat.class);
c.createAlias("mother.kind", "motherKind");
c.addOrder(Order.asc("motherKind.value"));
return c.list();
关于java - ORDER BY 使用 Criteria API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1780129/