0

I'm using jpa-named-queries.properties file for writing HQL and native queries for my repository interfaces.

now the problem here is when i write native query, returned values i cant directly cast into Entity as column defined as Long in entity are now changed to BigInteger. PFB

enter image description here

i got the solution also for this to cast native query to my entity but i want my query to stay in jpa-named-queries.properties , i dont want it to hardcode in my DAO implementation.

Query q = getEntityManager().createNativeQuery("select * from todo", Todo.class);
    List<Todo> l = q.getResultList();

above code works fine i.e. i can write native query and it will return data in entity format also.

is there any way i can read this query from jpa-named-queries.properties ???

jpa-named-queries.properties file

TodoRepo.getList=select * from todo

TodoRepo.java

@Repository
public interface TodoRepo extends JpaRepository<Todo, Long> {
      @Query(nativeQuery = true)
      public void getList();
}
  • First, try to read from the properties file([Ref](https://stackoverflow.com/questions/38281512/how-to-read-data-from-java-properties-file-using-spring-boot)) then pass the string in `createNativeQuery` – Eklavya Sep 05 '20 at 14:10
  • Thanks @Rono for suggestion, one doubt as already this file is loaded in context and Repository interfaces are reading this file, is it good idea to load same property file as a property source ?? just want to understand if loading it twice for different purpose will cause any performance problems or any thing like that ? – ankur singh Sep 05 '20 at 14:38
  • If you already loaded in context then use [like](https://stackoverflow.com/a/44996862/4207306) – Eklavya Sep 05 '20 at 14:45
  • thats the problem , i know it is in context as repository interfaces are reading it but, i don't know how to read it as it was picked-up by spring boot automatically once i placed it in **\src\main\resources\META-INF\jpa-named-queries.properties** . any suggestion how can i read this file ? – ankur singh Sep 05 '20 at 15:16
  • What happened if you use `@Query(name = "TodoRepo.getList",...` ? I am not sure about it's worked for JPQL only or not. – Eklavya Sep 05 '20 at 15:18
  • yes it works but again i'll not be able to map returned values to todo entiity class , all Long values are being converted to BigInteger.(as attached above image) i just need to pass this query in getEntityManager().createNativeQuery() method to work it properly. – ankur singh Sep 05 '20 at 15:30

0 Answers0