Named Query in XML File

Hello Team,

I wanted to write Named Queries in xml files and access the same using DataManager or EntityManager or by any other possible way you could suggest in my application. I have tried NamedQueries with annotations and its working fine. However at the same time when I tried with Named Queries in xml the jmix is not finding the named queries and throwing exception “The named query with the given name cannot be found” something like this.

I have written the named queries in orm.xml file and placed this file in src/main/resources/META-INF folder but still jmix can’t pickup.

Please assist. Thanks

1 Like

Hi Shyam,

I’m afraid specifying named queries or other metadata in orm.xml is impossible because Jmix creates persistence.xml and orm.xml files automatically at build time and it just ignores the file supplied by you.

What is your motivation for using named queries and placing them to an XML file?

Hello and thanks for your response.

We are developing solution which should be database agnostic. And what I have learned is that there are database specific queries/scripts which can’t be written in JPQL/HQL & hence we wanted to externalize these DB specific queries which would eventually may solve our problem to some extent. Although, while deployments, we may have to supply right named query/orm.xml file (which will have all the named queries) to achieve this.

Please let us know your thoughts on this.

Thanks for the explanation.
I think JPA named quieries is a too rigid solution for your task. You would be able to use them only in EntityManager, which is not a main approach to data access in Jmix. Normally you should be using DataManager most of the time. Also, the JdbcTemplate could be used instead of EntityManager for native queries.
As an example, see jmix-samples/stored-procedures-sample for different ways of calling stored procedures.

So I would suggest creating your own file sctructure in src/main/resources for your DB-specific JPQL and SQL queries. You can load them using the io.jmix.core.Resources bean depending on the database currently in use.