I've got a table - let us refer to it as AAA, with a one-to-many definition for an additional table, designated BBB. It appears something of that nature:

public class AAA
{
...
   @OneToMany(orphanRemoval=true, fetch=FetchType.LAZY, mappedBy="aaa")
   private List<BBB> bbbs;
...
}

I wish to operate a JPA query which will produce all of the AAAs which have several BBBs, and so i used SIZE(aaa.bbbs). However, I would like the end result set to become purchased through the timestamp from the first bbb in every aaa.

A genuine-existence example: I wish to get my facebook buddies, purchased through the latest picture each one has put into their profile. AAA within this situation is really a "Friend", and BBB is going to be "Picture", and I wish to order buddies by Picture.CreatedTimestamp.

I essentially have to access the very first item within the bbbs for every aaa, and order because of it. What ought to be done to be able to make that happen goal?

So, you'll need two aggregates over bbbs Body to look for the size and the other to obtain the latest timestamp. It may be expressed with JOIN and GROUP BY:

SELECT a
FROM AAA a JOIN a.bbbs b
GROUP BY a.id, a.column1, a.column2
HAVING COUNT(b) > 1
ORDER BY MAX(b.timestamp) DESC

If you work with Hibernate, maybe you should use Filters (@Filter and @FilterDef)