Search code examples
jpatoplink

JPA XML Query File


I would like to include all named queries in a separate XML file and use it in persistence. How can I accomplish the same?

I am using Toplink JPA in a JSF-Web application. Actually, I am not able to get the directory/file structure to use.

I tried to use <mapping-file> in persistence.xml and tried to place the query file in several locations. Every time I am getting exception.


Solution

  • You can either place to XML file queries that later will be executed by

     EntityManager.createQuey(query)
    

    where query is provided by XML.

    Or, the better way(!), use already existing possibility to declare JPA parts in mapping XML (example see define named query in orm.xml with jpa and hibernate). In the last case you can even use:

     EntityManager.createNamedQuery(queryName)