How to add global where clause for all find methods of Spring data JPA with Hibernate?(如何使用 Hibernate 为 Spring data JPA 的所有查找方法添加全局 where 子句?)
问题描述
我们正在开发使用带有休眠功能的 Spring data JPA 的 Web 应用程序.
We are working on web application using Spring data JPA with hibernate.
在应用程序中,每个实体都有一个compid字段.这意味着在每个 DB 调用(Spring Data 方法)中都必须使用 compid 检查.
In the application there is a field of compid in each entity. Which means in every DB call (Spring Data methods) will have to be checked with the compid.
我需要一种方法,即where compid = ?"检查是否为每个查找方法自动注入.这样我们就不必专门为 compid 检查而烦恼.
I need a way that, this "where compid = ?" check to be injected automatically for every find method. So that we won't have to specifically bother about compid checks.
这可以通过 Spring Data JPA 框架实现吗?
Is this possible to achieve from Spring Data JPA framework?
推荐答案
也许 Hibernate 的注解 @Where
会对你有所帮助.它将传递的条件添加到与实体相关的任何 JPA 查询中.例如
Maybe Hibernate‘s annotation @Where
will help you. It adds the passed condition to any JPA queries related to the entity. For example
@Entity
@Where(clause = "isDeleted='false'")
public class Customer {
//...
@Column
private Boolean isDeleted;
}
更多信息:1,2
这篇关于如何使用 Hibernate 为 Spring data JPA 的所有查找方法添加全局 where 子句?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何使用 Hibernate 为 Spring data JPA 的所有查找方法添加全局 where 子句?


基础教程推荐
- “未找到匹配项"使用 matcher 的 group 方法时 2022-01-01
- 降序排序:Java Map 2022-01-01
- Java Keytool 导入证书后出错,"keytool error: java.io.FileNotFoundException &拒绝访问" 2022-01-01
- 减少 JVM 暂停时间 >1 秒使用 UseConcMarkSweepGC 2022-01-01
- 设置 bean 时出现 Nullpointerexception 2022-01-01
- FirebaseListAdapter 不推送聊天应用程序的单个项目 - Firebase-Ui 3.1 2022-01-01
- 在 Libgdx 中处理屏幕的正确方法 2022-01-01
- 无法使用修饰符“public final"访问 java.util.Ha 2022-01-01
- 如何使用 Java 创建 X509 证书? 2022-01-01
- Java:带有char数组的println给出乱码 2022-01-01