本文介绍了ORDER BY使用Criteria API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
当我编写一个HQL查询的时候 查询q = session.createQuery(SELECT cat from cat as cat ORDER BY cat .mother.kind.value);
return q.list();
一切都很好。但是,当我编写一个Criteria时,我们可以使用标准C / C ++语言编写一个标准库。
c.addOrder(Order.asc(mother.kind.value));
返回c.list();
我得到一个异常 org.hibernate.QueryException:无法解析属性: kind.value:my.sample.data.entities.Cat
如果我想使用Criteria and Order,应该如何表达我的order by?
解决方案
您需要为 mother.kind $创建别名C $ C>。你这样做。
标准c = session.createCriteria(Cat.class);
c.createAlias(mother.kind,motherKind);
c.addOrder(Order.asc(motherKind.value));
返回c.list();
When I write a HQL query
Query q = session.createQuery("SELECT cat from Cat as cat ORDER BY cat.mother.kind.value");
return q.list();
Everything is fine. However, when I write a Criteria
Criteria c = session.createCriteria(Cat.class);
c.addOrder(Order.asc("mother.kind.value"));
return c.list();
I get an exception org.hibernate.QueryException: could not resolve property: kind.value of: my.sample.data.entities.Cat
If I want to use Criteria and Order, how should I express my "order by"?
解决方案
You need to create an alias for the mother.kind
. You do this like so.
Criteria c = session.createCriteria(Cat.class);
c.createAlias("mother.kind", "motherKind");
c.addOrder(Order.asc("motherKind.value"));
return c.list();
这篇关于ORDER BY使用Criteria API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!