Home > Mobile >  Dynamic where clause in custom query JPA
Dynamic where clause in custom query JPA

Time:08-28

How to pass where clause whitout using query dsl or custom repository?

public interface PlTransactionsRepository extends PagingAndSortingRepository<PlTransactions, Integer>, JpaSpecificationExecutor<PlTransactions> {
      @Query(value = "SELECT a FROM PlTransactions :whereClause")
      List<PlTransactions> selectTrxForRecon(@Param("whereClause") Integer whereClause);
}

CodePudding user response:

For dynamic where queries, you shouldn't use JPQL but Criteria API or the Spring Data JPA JpaSpecificationExecutor

If you implement JpaSpecificationExecutor you'll get find methods where you can pass a Specification. A Specification will form the dynamic where clause.

Please find more information in the documentation: https://docs.spring.io/spring-data/jpa/docs/current/reference/html/#specifications

  • Related