如何在Spring数据JPA中执行AND和多个OR参数方法 [英] how to do AND and multiple OR parameters method in spring data JPA

查看:2336
本文介绍了如何在Spring数据JPA中执行AND和多个OR参数方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为该查询制定方法名称:

I am trying to formulate a method name for this query :

  @Query("from Employees where department = ?1 and (fullTime = true or contractor = true or subContractor = true)")

我以为这种方法可以解决问题,但是在部门和全职工作时都可以使用and

I thought this method will do the trick, but it does an and on dept and full time

  public List<Employees> findByDepartmentAndfullTimeTrueOrContractorTrueOrSubContractorTrue(String dept);

这是一个相关的问题: Spring JPA数据或"查询,但在2012年被问到.是否有一种无需使用@Query即可实现此目标的方法?

This is a related question : Spring JPA Data "OR" query but was asked in 2012. Is there a way to achieve this without having to use @Query ?

推荐答案

当前不支持此功能,并且出于非常简单的原因可能永远不会:

This is currently not supported and probably never will be for a very simple reason:

派生的查询被认为是定义非常简单的查询的一种方式.我承认这是模糊的,但是如果您进入findByDepartmentAndfullTimeTrueOrContractorTrueOrSubContractorTrue,该是时候重新考虑这是否真的是您要向客户公开的内容了.写起来很笨拙,阅读起来很笨拙,实际上可能不仅仅是谓词的集合,但传达了更高层次的含义,因此应该以更具描述性的方式命名.

Derived queries are considered a means to define very simple queries. I admit this is blurry but if you get to findByDepartmentAndfullTimeTrueOrContractorTrueOrSubContractorTrue it's time to rethink whether that's actually what you want to expose to clients. It's awkward to write, awkward to read and probably actually more than a collection of predicates but conveying a higher-level meaning and thus should be named in amore descriptive way.

您已经发现的解决方案是使用@Query或Querydsl谓词.

The solution - as you already discovered - is to use @Query or Querydsl predicates.

这篇关于如何在Spring数据JPA中执行AND和多个OR参数方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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