0

I have a generic interface that extends the JpaRepository, I've overridden some methods and I want to create a new generic method to find records by field name and its value.

e.g :

@Query("SELECT gr FROM #{#entityName}  gr WHERE gr.#{#fieldName}=(:value) AND gr.deletedAt = NULL")
List<T> findByParam();

fieldName and value are @RequestParam objects.

{

    @Override
    @Query("SELECT gr FROM #{#entityName}  gr WHERE gr.deletedAt = NULL")
    List<T> findAll();

    @Override
    @Query("SELECT gr FROM #{#entityName}  gr WHERE gr.id=(:pId) AND gr.deletedAt = NULL")
    Optional<T> findById(@Param("pId") Integer pId);
}
Draken
  • 3,134
  • 13
  • 34
  • 54

1 Answers1

0

It seems like this has been answered in this thread :

Using generics in Spring Data JPA repositories

I wanted to write my own answer, but this one is already well written and should give you what you are looking for.

Nicolas
  • 103
  • 3
  • 12