Spring data jpa - 执行前修改查询 [英] Spring data jpa - modifying query before execution

查看:45
本文介绍了Spring data jpa - 执行前修改查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在做一个在数据库中保留一些访问控制信息的项目.我们正在使用该访问控制信息来过滤用户可以看到的内容.我们使用的过滤器基于用户拥有的角色.我们希望使用 Spring Data 提供的 Repository 抽象,但我们希望能够在执行之前调整生成的查询.有没有办法附加在执行查询之前将被调用的侦听器或拦截器?这样我们就可以获得对查询对象的引用,并在执行查询之前对查询进行任何需要的调整.

I'm working on a project that keeps some access control information in the database. We're using that access control information to filter what the user can see. The filter that we're using is based on roles that the user has. We would like to use the Repository abstraction provided by Spring Data, but we would like to be able to adjust the generated queries before they are executed. Is there a way to attach a listener or an interceptor that will be called before a query is executed? That way we can get a reference to the query object and make whatever adjustments to the query we need to before the query is executed.

我们正在考虑做的是创建我们自己的 JpaRepositoryFactoryBean 以便我们可以按照描述覆盖 SimpleJpaRepository 此处.我们将覆盖 SimpleJpaRepository.getQuery 以对查询进行调整.然后对于所有生成的 finder 方法,我们正在考虑扩展 PartTreeJpaQuery 并覆盖 PartTreeJpaQuery$QueryPreparer.在 QueryPreparer 中,我们将覆盖 QueryPreparer.createQuery 方法.我们不确定这是否是在执行之前访问所有查询的最简单方法.

What we're thinking about doing is creating our own JpaRepositoryFactoryBean so we can override SimpleJpaRepository as described here. We would override SimpleJpaRepository.getQuery to make adjustments to the query. Then for all the generated finder methods we were thinking about extending PartTreeJpaQuery and overriding the PartTreeJpaQuery$QueryPreparer. In the QueryPreparer we would override QueryPreparer.createQuery methods. We weren't sure if that was the simplest way to get access to all queries before they get executed.

我们考虑添加一个 org.springframework.data.repository.core.support.QueryCreationListener,但它只会在创建查询时执行.我认为我们需要一些更有活力的东西.

We thought about adding a org.springframework.data.repository.core.support.QueryCreationListener, but it would only get executed when the query is created. I think we need something more dynamic.

推荐答案

我不确定您是否已经知道,但 Spring Data 团队正在为下一个版本开发该功能,以使 Spring Security 团队能够然后添加对 ACL 的支持.

I'm not sure if you already know, but Spring Data team is working on that feature for the next release, to make it possible for Spring Security Team to add the support for ACLs then.

为通用查询增强添加基础结构

之前被阻止的 Spring Security 问题:Spring Security/Spring Data Acl 集成

Spring Security issue that is blocked by the previous: Spring Security / Spring Data Acl Integration

在我的公司中,我们创建了一个 JpaRepositoryFactoryBean,它将创建一个自定义 Repository 以允许我们为 ACL 添加过滤器,但我们只是为 findAll 和 findOne 方法这样做,所以我们失去了 Spring 的许多功能数据并决定恢复该更改,我们仍在考虑自动执行此操作的方法.如果我们发现它太难了,我认为我们会将这个责任委托给存储库的客户端,然后等待 Spring Security/Data 的支持.

In my company, we created a JpaRepositoryFactoryBean that would create a custom Repository to allow us to add the filter for the ACLs, but we just did that for the findAll and findOne methods, so we were loosing a lot of features of Spring Data and decided to revert that change and we're still thinking in a way to do it automatically. If we find out that it'll be too difficult, I think that we'll delegate that responsibility on the clients of the repositories and wait for the support for it in Spring Security/Data.

原来的问题已经有 1 年了.你找到了一种干净的方法吗?

The original question already has 1 year. Did you find a clean way to do it?

这篇关于Spring data jpa - 执行前修改查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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