Use "WHERE IN()" clause in SPRING BOOT JPA?

Asked

Viewed 955 times

1

Does anyone know how I can use any clause similar to WHERE IN in the spring jpa?

example:

@Modifying
@Transactional
@Query(value = "Delete from table where id in(:ids)",nativeQuery=true)
    void deleteByIdid(@Param("ids") String var);

I’ve tried to send a String similar to this:

5151,5151,51515,884,8484.

But it doesn’t work.

Message

couldn't execute statement.

I believe it’s time to pass the spring puts the variável in quotes.

1 answer

3

You can use spring data and do Dynamic queries https://stackoverflow.com/a/18996367/4219136

Example:

findByInventoryIdIn(List<Long> inventoryIdList)

You can find more information in the Spring Data documentation - http://bit.ly/1XADXf2

If you prefer to continue using query methods, I think you will need to change the parameter to a list, example:

@Query("SELECT t FROM Table t WHERE t.id IN :ids")
Set<Table> findByTableIn(Set<Long> ids);

However, I believe that the first option is preferable :)

You can adapt this option to your DELETE

  • 1

    that’s exactly what I needed, thank you.

  • @Robertooliveira happy to help! You can mark the answer as correct? :)

  • 1

    @Robertooliveira can mark the answer as correct?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.