javasqlspring-bootr2dbcnativequery

How to correctly compose a query with r2dbc @Query for three value options: true, false, null ? (postgresql)


I'm trying to make a request to get data from the database

The boolean value of three options comes into the parameters:

  1. false - show entities with false
  2. true - show entity with true
  3. null - any option corresponding to user_id

But I can't figure out what to do with null

@Repository
public interface Repo extends ReactiveCrudRepository<Device,Long> {

    @Query("SELECT * FROM device where user_id = $1 and show = $2")
    Flux<Device> findByUserIdAndShow(String userId, Boolean show);

}

Solution

  • @Query("SELECT * FROM device where user_id = $1 and ($2 is null or show = $2)")