How do I turn item ordering HQL into a filter query?

╄→尐↘猪︶ㄣ 提交于 2019-12-11 05:01:02

问题


I have this query:

sess.createQuery("from Box b join b.items bi order by bi.name").list()

It works fine. However, I have a hibernate's Collection boxes and want to filter is. Naive tries:

 sess.createFilter(boxes, "join this.items bi order by bi.name").list()
 sess.createFilter(boxes, "from this join this.items bi order by bi.name").list()

don't work!

What's the proper way to convert this HQL to filter?


回答1:


Try...

session.createFilter(items.getBoxes(), "order by this.name").list()



回答2:


When writing collection filters, this refers to collection element.

You can write something like:

sess.createFilter(boxes, "where this.name = ?").list();

That said, I don't see any conditions in your example. I'm not sure whether order by is allowed in collection filters (haven't tried it), but if all you want to do is sort collection elements you can specify sort order via @OrderBy annotation:

@OrderBy("name")
private List items;


来源:https://stackoverflow.com/questions/1465888/how-do-i-turn-item-ordering-hql-into-a-filter-query

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!