Pass native query per parameter to JpaRepository Interface

1

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

 public interface MeuRepository extends JpaRepository<MeuEntity, Integer>{

     @Query(value = query, nativeQuery = true)
     List<MeuEntity> findCustomNativeQuery(String query);
 }
    
asked by anonymous 11.05.2017 / 22:08

1 answer

0

In this case you do not have to, you have to use EntityManager , here's an example:

@PersistenceContext
protected EntityManager manager;

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