开发者

Call an HQL namedQuery from within another namedQuery?

Is it possible to call a named query from within another named query? If so please throw a quick example in your answer.

Context:

I have a subquery that will be repeated accross some other queries and don't want to make a maintence nightmare.

@NamedQuery(name = "Album.getSong", query = "SELECT song_name FROM Album" +
"WHERE album_name = ‘And Justice for All’" +
"AND band_name = ‘Metallica’" +
"AND song_name NOT IN" +
"(SELECT song_name FROM Lyric" +
"WHERE song_lyric LIKE ‘%justice%’)")

except that I will be reusing

SEL开发者_StackOverflowECT song_name FROM Lyric
WHERE song_lyric LIKE ‘%justice%’

in a number of named queries.


I don't think you can re-use named queries in named queries, there might be something available in your ORM vendor that makes this possible. However you can reference static strings in your named query for re-use.

@NamedQuery(name = "SELECT song_name FROM Album" +
"WHERE album_name = ‘And Justice for All’" +
"AND band_name = ‘Metallica’" +
"AND song_name NOT IN" + ExampleClass.QUERY)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜