Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
354 views
in Technique[技术] by (71.8m points)

jpql - JPA only executes first select statement in union?

I've written a JPQL statement that unions multiple select statements, each of which selects a different string constant. A simplified version of the query would look something like this:

SELECT DISTINCT 'A' AS widget_type
  FROM WidgetA widgetA
  WHERE widgetA.creationTimestamp > :cutoff
UNION SELECT DISTINCT 'B' AS widget_type
  FROM WidgetB widgetB
  WHERE widgetB.creationTimestamp > :cutoff

The query doesn't cause any errors but I'm not getting the result I expect. I see that the generated SQL doesn't have any unions - it only queries the table from the first select:

select distinct 'A' as col_0_0_ 
from widget_a widget0_
where widget0_.creation_timestamp>?

Is there an obvious reason why JPA would disregard everything after the first select statement? If it makes any difference, I am using Hibernate 4.1.9 as the JPA implementation, with a MySQL database.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

JPQL has no such concept as UNION. So consequently any "query" that has it is not JPQL and so ought to be rejected as invalid JPQL (which DataNucleus JPA certainly would do)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...