@NamedQuery覆盖Spring Data Rest JpaRepository中的findAll [英] @NamedQuery override findAll in Spring Data Rest JpaRepository

查看:132
本文介绍了@NamedQuery覆盖Spring Data Rest JpaRepository中的findAll的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有一种方法可以替代由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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆