从 属性 文件加载 JPA 存储库查询

Load JPA repository queries from a property file

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

@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" 字符串。有什么办法吗?

如上所述,我已经通过使用 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

<?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中我分别定义了所有的本地查询。当我需要在不更改 class 文件的情况下更改查询时,我只需要提及我需要在 persistence.xml.

中使用的文件名