如何在Spring JPARepository中复制Hibernate的@Where功能 [英] How to replicate the Hibernate's @Where functionality in Spring JPARepository

查看:88
本文介绍了如何在Spring JPARepository中复制Hibernate的@Where功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们的一个实体是使用@Where注释定义的,用于处理软删除".

One of our entities is defined using the @Where annotation to handle "soft-deletes".

@Entity
@Where(clause="user_type_cd != 'X'")
public class User {
...

现在,我们意识到有时我们仍然希望找到这样的实体.不幸的是,@ Where注释按设计工作,因此我们在JPARepository中定义的查找器无法找到已删除的用户.

Now, we are realizing that at times, we still want to find such entities. Unfortunately, the @Where annotation is working as designed and so the finders we have defined in JPARepository do not work to find deleted Users.

例如,

@Repository("userRepo")
public interface UserRepository extends JpaRepository<User, Long> {
    User findByLoginId(String login);
}

如果已删除用户,则如果user_type_cd等于'X',则findByLoginIs()方法将不会返回用户.

If the user has been deleted, then the findByLoginIs() method will not return a user if user_type_cd equals to 'X'.

但是,有时我们仍然想找到这样的用户,所以我当时想移动

However, at times, we still want to find such a user, so I was thinking of moving the

@Where(clause="user_type_cd != 'X'")

从实体到存储库的

注释.这样,我不必更新存储库中的所有方法,它会自动将此条件添加到所有查找器中.然后,我可以创建另一个存储库,该存储库将查找所有用户,而无需考虑软删除.

annotation from the entity into the repository. This way, I don't have to update all the methods in the repository and it would automatically add this criterium to all the finders. Then, I can create another repository which will find ALL the Users without regard to soft deletes.

但是,我无法找出使所有发现者自动附加

However, I am not able to figure out the exact method that I can use to make all of the finders automatically append the

clause="user_type_cd != 'X'"

存储库中所有方法的标准,而无需为每个查找器方法显式编写自定义@Query("... and user_type_cd!='X'").

criteria to all methods of the repository without me having to explicitly write a custom @Query("... and user_type_cd != 'X'") for each finder method.

因此,简而言之,我正在寻找所有自动生成的findBy以及将user_type_cd!='X'附加到所生成的每个查找中的此类方法.

So, in short, I am looking for all the auto-generated findBy and such methods to append the user_type_cd != 'X' to each find that is generated.

有没有实现自定义@Query注释的方法的想法吗?

Any ideas on how to accomplish this without resorting to custom @Query annotation?

推荐答案

我相信您正在寻找的是其中之一:

I believe what you are looking for is one of either:

  1. spring data jpa规范,或
  2. Querydsl
  1. Spring data jpa specifications, or
  2. Querydsl

通过任一简单的Google搜索,都有大量信息.这是一个有用的 spring.io博客,同时提供这两者的详细信息以及如何在JpaRepository中使用它们.

There's a wealth of information with a simple google search on either. Here's a useful spring.io blog with details on both, and how to use them with a JpaRepository.

这篇关于如何在Spring JPARepository中复制Hibernate的@Where功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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