[英]Pass native query per parameter to JpaRepository Interface
I need to create an interface that extends the JpaRepository where I want to pass a native (select) query
by parameter instead of leaving static within the @Query
annotation. 我需要创建一个扩展JpaRepository的接口,在该接口上我想通过参数传递本机(选择)
query
,而不是在@Query
批注中保持静态。 Instead of using @Query (value = "select * from a where a =: a", nativeQuery = true)
I want to use the code sampled below. 而不是使用
@Query (value = "select * from a where a =: a", nativeQuery = true)
我想使用以下@Query (value = "select * from a where a =: a", nativeQuery = true)
代码。
public interface MeuRepository extends JpaRepository<MeuEntity, Integer>{
@Query(value = query, nativeQuery = true)
List<MeuEntity> findCustomNativeQuery(String query);
}
Is it possible to do as the code exemplified above? 是否可以按照上面示例的代码进行操作? How?
怎么样?
If you MUST use native queries then do it with custom implementation. 如果必须使用本机查询,请使用自定义实现来执行。
public interface MeuRepositoryCustom {
List<MeuEntity> findCustomNativeQuery(String query);
}
then 然后
public class MeuRepositoryImpl implements MeuRepositoryCustom{
@PeristenceContext
private EntityManager em; // here you will get plain EntityManager impl.
List<MeuEntity> findCustomNativeQuery(String query){
TypedQuery<MeuEntity> q=em.createQuery(query,MeuEntity.class)
return q.getResultList();
}
}
finally your repository interface 最后是您的存储库界面
public interface MeuRepository extends JpaRepository<MeuEntity, Integer>, MCustomRepository{
}
Notice that naming is crucial here, as custom interface has to be named ***Custom
and its implementation has to be ***Impl
请注意,这里的命名至关重要,因为自定义接口必须命名为
***Custom
,其实现必须是***Impl
For more information https://docs.spring.io/spring-data/data-commons/docs/1.6.1.RELEASE/reference/html/repositories.html point 1.3 有关更多信息, 请访问https://docs.spring.io/spring-data/data-commons/docs/1.6.1.RELEASE/reference/html/repositories.html 1.3点
Here is newer version of documentation 这是文档的较新版本
https://docs.spring.io/spring-data/jpa/docs/current/reference/html/#repositories.custom-implementations https://docs.spring.io/spring-data/jpa/docs/current/reference/html/#repositories.custom-implementations
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.