Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UNION to JPA Query

Tags:

java

jpa

criteria

Is it possible to query "UNION" in JPA and even "Criteria Builder"?

I'm looking for examples, but so far i got no result.

Does anyone have any examples of how to use it?

Or would that be with native sql?

like image 324
Giovane Avatar asked Sep 23 '13 11:09

Giovane


People also ask

Can I use union in JPA query?

SQL supports UNION, but JPA 2.0 JPQL does not. Most unions can be done in terms of joins, but some can not, and some are more difficult to express using joins. EclipseLink supports UNION.

What is Nativequery JPA?

Native query refers to actual sql queries (referring to actual database objects). These queries are the sql statements which can be directly executed in database using a database client.


2 Answers

SQL supports UNION, but JPA 2.0 JPQL does not. Most unions can be done in terms of joins, but some can not, and some are more difficult to express using joins.

EclipseLink supports UNION.

like image 107
Masudul Avatar answered Oct 04 '22 10:10

Masudul


Depending on the case, one could use sub queries, something like:

select e from Entity e where e.id in (   select e.id   from Entity2 e2        join e2.entity e   where e2.someProperty = 'value' )       or e.id in (   select e.id   from Entity3 e3        join e3.entity e   where e3.someProperty = 'value2' ) 
like image 37
szczepanpp Avatar answered Oct 04 '22 09:10

szczepanpp