Blazebit / blaze-persistence

Rich Criteria API for JPA providers
https://persistence.blazebit.com
Apache License 2.0
727 stars 85 forks source link

Allow selectNew in set operations #565

Open Mobe91 opened 6 years ago

Mobe91 commented 6 years ago

Currently we prevent the use of selectNew in set operations although it actually is a valid use case. There are different strategies to do this:

MacNetron commented 2 years ago

I ran into this issue , expecting an ArrayList<MyResult> instead of getting an ArrayList<Object[n]> while doing a union(). See: https://stackoverflow.com/questions/74042943/blaze-persistence-how-to-get-real-objects-from-getresultlist

Proposed workaround, wrapping the union() in a with(), does work:

CriteriaBuilder<MyResult> cb = cbf.create(entityManager, MyResult.class)
  .with(MyResultCTE.class, false)
    .bind(..).select(..)
    .bind(..).select(..)
    ...
    .from(AClass.class)
    .union()
    .bind(..).select(..)
    .bind(..).select(..)
    ...
    .from(BClass.class)
    .endSet()
  .end()
  .selectNew(MyResult.class) 
    .with(..)
    .with(..)
    ...
  .end()
  .from(MyResultCTE.class);
return cb.getQuery.getResultList();