从属性文件加载JPA存储库查询 [英] Load JPA repository queries from a property file

查看:266
本文介绍了从属性文件加载JPA存储库查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要从属性文件中加载存储库中的查询。例如,这里:

I need to load queries in a repository from a property file. For example, here:

@Repository
public interface StudentRepository extends JpaRepository<Student, Integer> {

    @Query(value="SELECT * FROM student where year= :le", native=true)
    public List<Student> getStudentsByLevel(@Param("le") int level);

}

我需要加载 SELECT * FROM student where year =:le来自属性文件的字符串。有没有办法做到这一点?

I need to load the "SELECT * FROM student where year= :le" string from a property file. Is there any way to do it?

推荐答案

如上所述,我通过使用xml配置的命名查询克服了这个问题。以下是示例。

As suggested above I have overcome this problem by using xml configured named queries. Below is the example.

persistence.xml

persistence.xml

<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd"
    version="1.0">

    <persistence-unit name="hibernatePersistenceUnit">

        <!-- <mapping-file>META-INF/jpql/oracle-quries.xml</mapping-file> -->
        <mapping-file>META-INF/jpql/mysql-quries.xml</mapping-file>
    </persistence-unit>
</persistence>

mysql-quries.xml

mysql-quries.xml

<?xml version="1.0" encoding="UTF-8"?>
<entity-mappings version="1.0"
    xmlns="http://java.sun.com/xml/ns/persistence/orm" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/persistence/orm http://java.sun.com/xml/ns/persistence/orm_1_0.xsd ">

    <named-native-query name="Nodecontrol.lockNodeController">
        <query>
            SELECT * FROM student where year= :le
        </query>
    </named-native-query>
</entity-mappings>

在oracle-quries.xml和mysql-quries.xml中我分别定义了所有本机查询。当我需要在不更改类文件的情况下更改查询时,我只需要提及我需要在persistence.xml中使用的文件名。

In oracle-quries.xml and mysql-quries.xml I have defined all the native queries respectively. When I need to change the query without changing the class file I just need to mention the file name i need to use in persistence.xml.

这篇关于从属性文件加载JPA存储库查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆