1

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?

alamar
  • 18,729
  • 4
  • 64
  • 97

2 Answers2

2

Try...

session.createFilter(items.getBoxes(), "order by this.name").list()
dira
  • 30,304
  • 14
  • 54
  • 69
0

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;
ChssPly76
  • 99,456
  • 24
  • 206
  • 195
  • No I can't write something like this, because I can't introduce a join that easily. What you really mean is "order by this.items.name", but it won't work. And no, I can't specify sort order via @OrderBy, because it's an order, not the order: it's different each time. And you can use order by in filters. – alamar Sep 23 '09 at 18:26
  • Sorry, I misread your question. So you're trying to order a collection by a property of its own collection element - that won't work as part of the filter. To be honest, I'm not quite sure why it would even make sense - if one "box" has items A and B and the other A and C the resulting ordering will be random. – ChssPly76 Sep 23 '09 at 18:38
  • I've rewrote this query to still be a query, it's ugly but it does work. The question of sense is the another question. – alamar Sep 24 '09 at 08:09