1

It will be simple, but I can not figure out how to fix it. I want to select all records when query parameter "empresaId" is NULL not like now no record is selected. That is when "empresaId" is null is as if there were such a condition.

   @Query("select paquete from Paquete paquete where paquete.empresa.id =:empresaId")
   Page<Paquete> advancedSearch(@Param("empresaId") Long empresaId, Pageable pageable);

Can somebody help me. Thank you very much.

Neil Stockton
  • 11,383
  • 3
  • 34
  • 29
Jose
  • 1,779
  • 4
  • 26
  • 50
  • This may help http://stackoverflow.com/questions/13474207/sql-query-if-parameter-is-null-select-all same issue but in different manner – Issam El-atif Oct 13 '16 at 14:46
  • 2
    Try `@Query("select paquete from Paquete paquete where paquete.empresa.id =:empresaId or :empresaId is null")` – Pau Oct 14 '16 at 09:28
  • I used to like @Query(value = "SELECT COUNT(*) FROM User u Where (u.modifier != 'generator' or u.modifier is null) and u.type=2") – katsu Oct 14 '16 at 11:49

1 Answers1

1

In this case the easy way is to add an or condition checking if the param is null so in this case it will return true and all rows will be fetched.

@Query("select paquete from Paquete paquete where paquete.empresa.id =:empresaId or :empresaId is null")

If there are more dynamic conditions you could take a look to Query By Example which allows dynamic query creations.

Pau
  • 14,917
  • 14
  • 67
  • 94