Home > Blockchain >  How to use undetermined number of parameters in JPA
How to use undetermined number of parameters in JPA

Time:08-27

My current function is like this

public interface CartDAO extends JpaRepository<CartBean, Long>{
@Modifying
    @Transactional
    @Query(value = "UPDATE cart SET bought =1 WHERE id=?" ,nativeQuery = true)
    void buy(Long id);
}

If I want to pass undetermined number of ids to SQL for query, how can I write ?

like this

"UPDATE cart SET bought =1 WHERE id=1 OR id=2 OR id=3 ..."

@Modifying
    @Transactional
    @Query(value = "UPDATE cart SET bought =1 WHERE id=?..." ,nativeQuery = true)
    void buy(Long id...);

CodePudding user response:

"UPDATE cart SET bought =1 WHERE id=1 OR id=2 OR id=3 ..."

This is what you want.

@Modifying
@Transactional
@Query(value = "UPDATE cart SET bought =1 WHERE id IN :ids" ,nativeQuery = true)
void buy(@Param("ids") List<Long> ids);
  • Related