jpaillegalargumentexception

Encountered array-valued parameter binding, but was expecting [java.lang.Integer (n/a)]


@Query("SELECT adv FROM Advocacyadv where (adv.patient.facilityId IN (:facilityIds) or :facilityIds is null) "
            + " order by adv.createdDate desc ")
    public List<Advocacy> search(
            @Param("facilityIds") Integer[] facilityIds);

The above code gives the following error

Caused by: java.lang.IllegalArgumentException: Encountered array-valued parameter binding, but was expecting [java.lang.Integer (n/a)]

My search may or may not have search parameters. If there are no search parameters, I want all the data to show up. If there are search parameters, I only need those that match. How can I accomplish this using JPA?


Solution

  • You need to use a list

    Query("SELECT adv FROM Advocacyadv where (adv.patient.facilityId IN (:facilityIds) or :facilityIds is null) "
                + " order by adv.createdDate desc ")
        public List<Advocacy> search(
                @Param("facilityIds") List<Integer> facilityIds);
    

    Update:

    The correct query is as follows:
    Query(
        "SELECT adv FROM Advocacy adv " +
        "WHERE (:facilityIds IS NULL OR adv.patient.facilityId IN :facilityIds) " +
        "ORDER BY adv.createdDate DESC")
        public List<Advocacy> search(@Param("facilityIds") List<Integer> facilityIds);
    
    1. If facilityIds is null it will return all Advocacys
    2. If facilityIds is empty it will return an empty list
    3. Otherwise it will return all Advocacys that have a patient with an id in facilityIds