user1516815 user1516815 - 1 month ago 22x
Java Question

Load JPA repository queries from a property file

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

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


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


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


<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns=""

    <persistence-unit name="hibernatePersistenceUnit">

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


<?xml version="1.0" encoding="UTF-8"?>
<entity-mappings version="1.0"
    xmlns="" xmlns:xsi=""
    xsi:schemaLocation=" ">

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

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.