Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
358 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 Reply

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)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...