Search code examples
springspring-bootspring-data-jpaspring-data

Spring Boot JPQL Query


I need to send the complete query as parameter to repository to use it in @query, how can take the parameter in @query? i used this because generate the JPQL query in the code not same every time. `

@Query(/*query*/)
    List<Job> getJob(@Param("query") String query);

I used this way but this occurred error because transform the JPQL to SQL not I want to transform it.

    @Query(value="#{query}", nativeQuery = true)
    List<Job> getJob(@Param("query") String query);

Solution

  • Solve the problem by using Entity Manager another way to implement the query, in the first built interface have the header for function (getJob) like this:

    List<Job> getJob(String query);
    

    then built class implements from the interface notice need to add repository annotation in the class and write this code:

    @PersistenceContext
    private EntityManager em;
    
    
    @Override
    public Object getJob(String query){
        return em.createQuery(query).getResultList()
    }