如何为Spring Data JPA中的所有查询创建动态WHERE子句? [英] How to create a dynamic WHERE clause for all queries in Spring Data JPA?

查看:54
本文介绍了如何为Spring Data JPA中的所有查询创建动态WHERE子句?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个需求,我需要动态地向所有查询传递全局 WHERE 条件?

I have a requirement where I need to pass a global WHERE condition dynamically to all the queries?

我们在表中有多个组织的数据.因此,我需要在检索数据时传递 ORG_ID =?.

We have data in the tables for multiple organizations. So, I need to pass ORG_ID = ? while retrieving data.

我不想编写查找器方法,例如 findBy..AndOrgId(... Integer orgId).有一个更好的方法吗?要应用全局 WHERE 子句还是谓词?

I don't want to write finder methods like findBy..AndOrgId(... Integer orgId). Is there a better way to do this? To apply a Global WHERE clause or a Predicate?

我在 RestController 中收到此 orgId 作为请求属性的一部分.由于它是动态的,因此我不能在实体类上使用休眠注释 @Where .

I receive this orgId as part of the request attribute in RestController. Since it is dynamic, I can't use the hibernate annotation @Where on the entity classes.

推荐答案

Spring Data(我假设您正在基于您的帖子标签使用Spring Data)不支持查询扩充(在撰写本文时).在此处,人们对该功能的要求一直很高,还介绍了其他功能的一些替代方法尝试过的.

Spring Data (I assume you are using Spring Data based on your post tags) doesn't support query augmentation (at time of writing). There is a long standing request for that feature here that also describes some alternatives that others have tried.

祝你好运!

这篇关于如何为Spring Data JPA中的所有查询创建动态WHERE子句?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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