Pass native query by parameter to Jparepository Interface

Asked

Viewed 576 times

1

I need to create an interface that extends Jparepository where I want to pass a native query(select) by parameter instead of leaving static inside the annotation @Query. Instead of using @Query(value = "select * from a where a = :a", nativeQuery = true) i want to use the code exemplified below.

 public interface MeuRepository extends JpaRepository<MeuEntity, Integer>{

     @Query(value = query, nativeQuery = true)
     List<MeuEntity> findCustomNativeQuery(String query);
 }

1 answer

0

In this case there is no way, you have to use the EntityManager, follows an example:

@PersistenceContext
protected EntityManager manager;

public List<MeuEntity> findCustomNativeQuery(String query) {
    return manager.createNativeQuery(query, MeuEntity.class).getResultList();
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.