Q:

UNION operator in Ebean (or valid alternatives)

In a Play project, I have two kinds of entities, A and B, and two relations, X and Y. X holds couples of A entities and a country field, while Y holds mixed A-B couples.  The entity B holds too a country field.  I need to make a query on A, which tells me if an A is the second entity in a relation X with X.country='US' or in a Y relation with Y.B.country='US'.  A working PostgreSQL statement for this query is:
 SELECT * FROM A WHERE A.id IN 
  (
   (SELECT X.a2_id FROM X WHERE country = 'US')
    UNION 
   (SELECT Y.a_id FROM Y WHERE b_id IN
    (SELECT B.id FROM B WHERE B.country = 'US')
   )
  );

With Ebean I managed to have each of the subqueries working, just I miss the union statement:
 Query subqueryX = X.find.select("a2.id")
        .where().eq("country","US").query();

 Query subqueryY = Y.find.select("a.id")
        .where().eq("b.country", "US").query();

 List<A> result = A.find.where().in("id", subquery).findList();

Here, find is play.db.ebean. Model. Finder<Id.class, Entity.class>, like in the computer DB template of Play.  I would like to combine the two subqueries in a unique one to put it in the subquery parameter of the result line.  
I've already tried to collect the ids from both the subqueryX and subqueryY, and then using the ExpressionList<T>.in(String propertyName, Collection<? > values), but then I got a java.sql. SQLException: Connection is closed!  if the Collection becomes too big (and it is usually the case). 
Any suggestion? 
EDIT
Wait, also the 
    List<A> result = A.find.where().in("id", subquery).findList();

is failing, giving me a java.sql. SQLException: Connection is closed!.  So the question is turned on how I implement the SQL query above in ebean? 
P.S. The connection closes when in the collection in the .in() method are around 32750 ids... 

A:

I see two solutions here:

Use RawSql to execute your SQL query.  
You have set of ids and you want a list of objects but calling .in() throws exception when collection is too big.  So you can split set of ids to smaller sets and call .in() for every set.  Then join result lists into one list.