Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to extract Spring Data JPA query to separate file

Spring Data JPA allows to extract jpql or sql queries to orm.xml as described in Spring Data Jpa Reference.

In such scenario multiple queries would end up in single orm.xml file. In our scenario this would lead to huge orm.xml as we have couple of huge queries.

I would like to achieve that each query would be stored in separate file, e.g. query for UserRepository findByLastname would be stored in META-INF/User/findByLastname.jpql or META-INF/User/findByLastname.sql if it is native query.

Is it possible to achieve such query-per-file extraction in Spring Data JPA?

PS: I am aware that query can be stored directly in Repository using @Query annotation but our maintenance team wants to have them extracted.

Thank you :-)

like image 754
ondrej.lerch Avatar asked Oct 29 '22 16:10

ondrej.lerch


1 Answers

I think in your case you could use multiple orm.xml files with different name/locations:

<persistence-unit name="app-unit" transaction-type="JTA">
    ...
    <mapping-file>mapping/orm-user.xml</mapping-file>
    <mapping-file>mapping/orm-settings.xml</mapping-file>
    <mapping-file>mapping/orm-data.xml</mapping-file>
    ...
</persistence-unit>
like image 58
hoaz Avatar answered Nov 15 '22 04:11

hoaz