Spring Crudrepository Findbyinventoryids(List<Long> Inventoryidlist) - Equivalent to in Clause

Spring CrudRepository findByInventoryIds(ListLong inventoryIdList) - equivalent to IN clause

findByInventoryIdIn(List<Long> inventoryIdList) should do the trick.

The HTTP request parameter format would be like so:

Yes ?id=1,2,3
No ?id=1&id=2&id=3

The complete list of JPA repository keywords can be found in the current documentation listing. It shows that IsIn is equivalent – if you prefer the verb for readability – and that JPA also supports NotIn and IsNotIn.

Convert sql to JPA with a in-clause and a list of like

A regex query could be one way to achieve this, as described here:
Searching multiple colums with a Contains query with a list of Items in Spring-Data JPA

Sample for MariaDB/MySQL:

@Query(nativeQuery = true, value = "SELECT * FROM my_entity WHERE "
+ "first_name REGEXP CONCAT_WS('|', :nameslist) " + "OR "
+ "last_name REGEXP CONCAT_WS('|', :nameslist) ")
List<MyEntity> findByNames( // (method name does not matter)
@Param("nameslist") Collection<String> nameslist);

(CONCAT_WS joins the provided list, e.g. ('val1', 'val2', 'val3') to the regex 'val1|val2|val3', which would match e.g. 'completeval2xxx'.)

Note that the exact REGEXP / CONCAT_WS functions depend on the used DB.

Spring Data Method Name for IN (Multiple Values)

Try this one
void deleteByIdentifierAndRoleIdIn(String identifier, long roleId);

It would helpful to you.

Spring Data with strict In-clause

Sorry, but your question is incorrect initially.

findAllByAttributeIn(Set<Attribute> setAttr)

produces an SQL request like this:

select * from <table_name> t where t.attribute in (?,?,?...?)

So according to RDBMS-rules your result is correct and expectable.
What is a real structure for your abstract tables?

 id | attr
----------
1 | A
2 | A,B
3 | A,B,C

In real life it would be somethink like this:

 id | attr
----------
1 | A
2 | A
2 | B
3 | A
3 | B
3 | C

And again - in this case the result you have got is ok for any RDBMS.
Give us real examples from your project, please

Update because of question update:

OK, the problem has been clarified.
In this case there is no straight way to solve it using standard CrudRepository syntax. But you can try to write @Query request to manage to get the goal.
In a clear SQL this problem has to be solved by using group by together with having. It would be something like this:

select media_id 
From media_has_tags
Where tag_id in(1,2 3)
Group by media_id
Having count(*)=3

In terms of SpringData it means that you have to create MediaHasTagsRepository interface and an appropriate query method to get looked ids. After that you can easily find medias you're looking for.
But this approach is not looking good IMHO. The best way I suppose is to find all the medias by the your initial query and than filter them by the given condition in Java.
E.g. you have a List<Media> where each element has an least a one tag that you are looking for. Then we can do a loop to find medias that contains all the looked tags:

List<Media> list; // here is a filled list of medias
Set<String> titles; // here is a set of title interseptions we a looking for
final List<Media> result = new ArrayList<>();
for (Media media: list) {
if (!list.getTags().isEmpty()){
Set<String> tagTitles = list.getTags().stream().map(item -> item.getTitle()).filter(title -> titles.contains(title)).distinct().collect(Collectors.toSet());
if (tagTitles.size() == titles.size()) {
result.add(media);
}
}
}


Related Topics



Leave a reply



Submit