Spring naming + jpa method

i have a simple JpaRespository where i have to use some methods with spring naming

it sql it looks like

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

in jpa same query will be look like this

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

and its work pretty good

and what i need to know ?how to create same query in spring naming method

in sql it looks like

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

i tried to do something like this

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

but it does not work correctly - it give me all records with updates less then this date but they are not in current list of user_ids and i know why - cuz its OrUpdatedAtLessThanEqual if i do AndUpdatedAtLessThanEqual it does not work correctly too

so is there any way to do what i need without create native query

Upvotes: 0

Views: 124

Answers (1)

DonAjit
DonAjit

Reputation: 46

What you have written findAllByUserIdInAndCreatedAtLessThanEqualOrUpdatedAtLessThanEqual is correct, but if you look closely in your native query, you will find parenthesis which groups the conditions.

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

Since, there is no feature to accommodate grouped conditions, you will have to use @Query annotation.

@Query annotation not only reduces the headaches of non-readability of long JPA method names, but also eliminates the use of joins (since you will use parameter names used in Entity instead column names in table), which in turn improves readability and reduces usage of Native queries.

Upvotes: 1

Related Questions