ondrej.lerch
ondrej.lerch

Reputation: 128

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 :-)

Upvotes: 5

Views: 2108

Answers (1)

hoaz
hoaz

Reputation: 10153

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>

Upvotes: 2

Related Questions