Spring命名+ jpa方法



我有一个简单的jparerepository,我必须使用一些方法与spring命名

它看起来像

select * from a where a.user_id in ( some id values)

在jpa相同的查询将看起来像这样

List<A> findAllByUserIdIn(List<Long>ids) 

和它的工作相当好

和我需要知道什么?如何创建相同的查询在spring命名方法

在SQL中看起来像

select * from a where a.user_id in ( some values...) and ( a.created_at <='some date" or a.updated_at<='some date)

我试过这样做

List<A> findAllByUserIdInAndCreatedAtLessThanEqualOrUpdatedAtLessThanEqual(List<Long>ids,TimeStamp created_at,TimeStamp  updated_at)

,但它不能正常工作-它给我所有的记录更新少于这个日期,但他们不在当前的user_ids列表,我知道为什么-因为它的OrUpdatedAtLessThanEqual如果我做AndUpdatedAtLessThanEqual,它也不能正常工作

那么有没有办法在不创建本机查询的情况下做我需要的事情呢

您所写的findAllByUserIdInAndCreatedAtLessThanEqualOrUpdatedAtLessThanEqual是正确的,但是如果您仔细查看本机查询,您会发现括号将条件分组。

Requirement - condition1 AND (condition2 OR condition3)
Your method name - condition1 AND condition2 OR condition3

由于没有适应分组条件的特性,您将不得不使用@Query注释。

@Query注释不仅减少了长JPA方法名不可读的头痛,而且还消除了连接的使用(因为您将使用Entity中使用的参数名而不是表中的列名),这反过来提高了可读性并减少了Native查询的使用。

最新更新