Does Spring Data JPA provide a query keyword to check if at least one item contained in a list match at least one of the items contained in another list.
Here's my query : (*** should be the keyword I'm looking for. Ex : contains, in , ... but I don't know which one is the right one.)
Page<ActivityEntity> findAllByHashtags***AndDateIsGreaterThanEqual(List<HashtagEntity> hashtags, LocalDateTime date, Pageable pageable);
Some more information : Here's my Activity entity.
public class ActivityEntity {
@Id
@GeneratedValue
private Long id;
private String name;
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd HH:mm a z")
private LocalDateTime date;
@ManyToMany(cascade=CascadeType.ALL)
@Cascade({org.hibernate.annotations.CascadeType.DELETE, org.hibernate.annotations.CascadeType.REMOVE})
private List<HashtagEntity> hashtags;
}
I'd like to retrieve every activity whom hashtags match at least one of the hashtags contained in a list.
For example : Given the activities contained in the database :
[
{
"id": 1,
"name": "Karaoke time",
"date": "2022-05-28T12:30:00Z",
"hashtags":
[
{ "name": "fun" }, { "name": "karaoke"}, { "name": "friend" }
]
},
{
"id": 2,
"name": "Just Dance Session V.1",
"date": "2022-05-28T12:30:00Z",
"hashtags":
[
{ "name": "fun" }, { "name": "dance"}
]
}
]
If I provide the following hashtags list :
[
{ "name": "karaoke" }, { "name": "heavy metal"}
]
I should receive :
[
{
"id": 1,
"name": "Karaoke time",
"date": "2022-05-28T12:30:00Z",
"hashtags":
[
{ "name": "fun" }, { "name": "karaoke"}, { "name": "friend }
]
}
]
I see there is another post on the subject : Spring JPA Query Check If At Least One Element of a List Exists in Parameter List
But I'd like to avoid this solution because this would require to make an additional costly query.
I actually managed to find a solution : The keyword "IN" works for what I want !
Page<ActivityEntity> findAllByHashtagsInAndDateIsGreaterThanEqual(List<HashtagEntity> hashtags, LocalDateTime date, Pageable pageable);