@NamedQuery 覆盖 Spring Data Rest JpaRepository 中的 findAll [英] @NamedQuery override findAll in Spring Data Rest JpaRepository
问题描述
有没有办法覆盖 Spring Data Rest 执行的 findAll 查询?
Is there a way to override the findAll query executed by Spring Data Rest?
我需要一种基于某些特定条件过滤结果的方法,似乎使用 @NamedQuery
应该符合我的要求,所以我设置了一个测试.
I need a way of filtering the results based on some specific criteria and it seems that using a @NamedQuery
should be along the lines of what I'm looking for so I setup a test.
@Entity
@Table(name = "users")
@NamedQueries({
@NamedQuery(name = "User.findAll", query="SELECT u FROM User u WHERE u.username = 'test'"),
@NamedQuery(name = "User.findNameEqualsTest", query="SELECT u FROM User u WHERE u.username = 'test'")
})
public class User implements Serializable, Identifiable<Long> { }
有了这个,我希望 SDR 使用我的 findAll() 查询(返回 1 个结果),但它执行相同的旧 findAll 逻辑(返回所有结果).
With this in place I would expect SDR to utilize my findAll() query (returning 1 result) but instead it executes the same old findAll logic (returning all results).
在我的存储库中,我添加了:
In my Repository I added:
@Repository
@RestResource(path = "users", rel = "users")
public interface UserJpaRepository extends JpaRepository<User, Long> {
public Page<User> findNameEqualsTest(Pageable pageable);
}
在这种情况下,它会选择提供的 @NamedQuery
.所以...
and in this case it DOES pick up the provided @NamedQuery
. So...
我应该如何覆盖默认的 findAll()
逻辑?我需要实际构建一组复杂的标准并将其应用于结果集.
How should I go about overriding the default findAll()
logic? I need to actually construct a complex set of criteria and apply it to the result set.
推荐答案
是的,你可以创建你的 Repository 接口的实现,
Yes, you can create your Implementation of your Repository interface, there is acouple section in
存储库
@Repository
public interface PagLogRepository extends JpaRepository<PagLogEntity, Long>, PagLogCustomRepository {
自定义界面
public interface PagLogCustomRepository {
PagLogEntity save(SalesForceForm salesForceForm) throws ResourceNotFoundException;
自定义实现
public class PagLogRepositoryImpl implements PagLogCustomRepository {
@Override
public PagLogEntity save(final SalesForceForm salesForceForm) throws ResourceNotFoundException {
query = emEntityManager.createNamedQuery("findItemFileByDenormalizedSku", ItemFileEntity.class);
query.setParameter("skuValue", rawSku);
使用 findAll 代替覆盖保存,然后您可以创建复杂的自定义
Instead of override save make it with findAll, then you can create complex customization
这篇关于@NamedQuery 覆盖 Spring Data Rest JpaRepository 中的 findAll的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!