如何将 Spring Data JPA 查询提取到单独的文件

ond*_*rch 5 java spring jpa spring-data spring-data-jpa

Spring Data JPA 允许按照Spring Data Jpa Reference 中的描述提取jpqlsql查询。orm.xml

在这种情况下,多个查询最终会出现在单个orm.xml文件中。在我们的场景中,这将导致巨大的,orm.xml因为我们有几个巨大的查询。

我想实现每个查询都存储在单独的文件中,例如查询 UserRepository findByLastname将存储在META-INF/User/findByLastname.jpqlMETA-INF/User/findByLastname.sql如果它是本机查询。

是否可以在 Spring Data JPA 中实现这种按文件查询的提取?

PS:我知道查询可以直接存储在 Repository使用@Query注释中,但我们的维护团队希望提取它们。

谢谢 :-)

hoa*_*oaz 2

我认为在您的情况下,您可以使用具有不同名称/位置的多个 orm.xml 文件:

<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>
Run Code Online (Sandbox Code Playgroud)