Turns out, I was not the only one asking this question and there is a very nice solution:
Storing the query text in a separate XML mapping file(s)
I base my solution with minor changes on Arjan's excellent post. There are, essentially, two steps:
1. Define a separate XML mapping file(s) in persistence.xml
The relevant line is
<mapping-file>META-INF/jpql_queries.xml</mapping-file>
It refers to the file containing the named queries. It is placed in the META-INF folder, which is where persistence.xml is located, too. I thought initially that since both persistence.xml and the query file (jpql_queries.xml) are in the same folder I can use
<mapping-file>jpql_queries.xml</mapping-file>
but that's wrong - than the query file is not retrieved during the build time.
2. Storing the named queries in the respective file
The syntax of the queries can be the same as when the named queries are stored in the file containing the JPA2 entity. In my case, I decided to use the advice included in Mkyong's excellent post, namely, to wrap the query text with CDATA, so that the XML parser will not prompt error for some special XML characters like ‘>’ , <’. For completeness sake I am including also the query file:
Great Info, keep up the good work
ReplyDelete