I have 2 entites, PERSON and PEOPLE
PERSON has columns namely -> FN, LN
PEOPLE has columns -> FIRST, LAST
I want to fetch the union of both the tables together. How do I do it in JPA. I have used the below way:
Created a new DTO -> Human having 2 fields FIRSTNAME and LASTNAME (case sensitive)
@Query(value=" SELECT FIRSTNAME, LASTNAME FROM "+
"( SELECT "+
" P.FN AS FIRSTNAME, "+
" P.LN AS LASTNAME " +
" FROM PERSON P"+
" UNION "+
" SELECT "+
" A.FIRST AS FIRSTNAME, "+
" A.LAST AS LASTNAME ' +
" FROM PEOPLE A"+
")", nativeQuery = true)
Pageable<Human> getEntireHumanRace() {
....
}
The SQL runs fine, but the ORM always forms a malformed SQL such as "Syntax error in SQL statement SELECT COUNT(P) FROM PERSON P ...." InvalidDataAccessResourceUsageException: could not prepare statement
Is there any suggestion on what can be done? Why does it put the count in front of the generated query? Appreciate in advance.
Why does it put the count in front of the generated query?
Because you are trying to get data with pagination (Pageable
). So for total element count Count query executing.
Is there any suggestion on what can be done?
You are using class-based projection and use List<Human>
@Query(value="SELECT FIRSTNAME, LASTNAME FROM ...")
List<Human> getEntireHumanRace();