@Query 不适用于 QueryDSL 谓词 [英] @Query not working with QueryDSL predicate

查看:93
本文介绍了@Query 不适用于 QueryDSL 谓词的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在 QueryDSL 的 Overrided 方法上使用 @Query,但是当我这样做时,它忽略了我提供的谓词例如http://localhost:8080/orders?state=Texas

I'm trying to use @Query over QueryDSL's Overrided method, but when i'm doing that, it is ignoring my predicate which i'm providing e.g. http://localhost:8080/orders?state=Texas

我需要 @Query 的原因是基于身份验证主体应用安全性,例如

The reason i need @Query is to apply security based on authentication principal e.g.

@Query("select o from Orders o where ?#{principal.username} = o.username")

仓库完整代码:

public interface OrderRepository extends JpaRepository<Order, Integer>, QuerydslPredicateExecutor<Order>{

    @Override
    @Query("select o from Orders o where ?#{principal.username} = o.username")
    Page<Order> findAll(Predicate predicate, Pageable pageable);
}

推荐答案

如果您想要基于提供的查询或基于 QueryDSL 谓词实现的方法,您必须下定决心.Spring Data 不会将它们组合起来(如您所见).

You have to make up your mind if you want a method implemented based on a provided query or based on a QueryDSL predicate. Spring Data doesn't combine them (as you found out).

在这种情况下,我假设您有充分的理由使用 QueryDSL,因此您应该只将基于主体的约束添加到该谓词中.

In this case, I assume you have a good reason to use QueryDSL, therefore you should just add the principal based constraint to that predicate.

这篇文章展示了如何访问主体在构建 Querydsl 谓词的任何地方使用.

This article shows how to access the principal which you can use wherever you are constructing your Querydsl predicate.

这篇关于@Query 不适用于 QueryDSL 谓词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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