0

What is the correct JPA syntax when using union and order by?

At the moment I have something in the following manner:

SELECT st FROM Student st JOIN TREAT (st.type as HighShcool) hs
UNION
Select st FROM Student st JOIN TREAT (st.type as MiddleShcool) ms
ORDER BY st.id

This is failing and the exception that I am getting is the end of the query is wrong i.e ORDER BY section. Without this, it is working.

Please help, I have no idea why it is happening.

ahmed
  • 9,071
  • 3
  • 9
  • 22
jaConfu
  • 47
  • 3
  • Does this answer your question? [UNION to JPA Query](https://stackoverflow.com/questions/18958614/union-to-jpa-query) – Jens Sep 20 '22 at 12:47
  • No, I have looked at this, the issue stems directly from JPA. Theortcally, when you create a query from the entity manager you would not be able to put a UNION into the request JPA content. But you can use StringBuilder to circumvent this and toString() it in. But it does not accept the ORDER BY. – jaConfu Sep 20 '22 at 12:57

0 Answers0