Spring Data JPA @Query with foreign key: Parameter not matched

Thadeu Antonio Ferreira Melo :

I have a table "Signal" with id, volume and object_id columns.

Object_id is a foreign key. I need to retrieve each signal that has a particular object_id.

Im trying to use this Query

public interface SignalRepository extends JpaRepository<Signal, Integer> {
    @Query("select s from Signal s where s.object = ?1")
    Optional<List<Signal>> findSignalByObjectId(Integer objectId);

}

It doens't work. If I change "?1" to 1 it gets the hardcoded value. If I try to query the "volume", it works fine.

I get this error:

Blockquote nested exception is java.lang.IllegalArgumentException: Parameter value [1] did not match expected type

Gaurav Agarwal :

You can use Spring data to generate the underlying query like this :

public interface SignalRepository extends JpaRepository<Signal, Integer> {
    List<Signal> findSignalByObjectId(Integer objectId);
}

or you can write the query with this return type and parameter:

public interface SignalRepository extends JpaRepository<Signal, Integer> {
    @Query("select s from Signal s where s.object = :id")
    List<Signal> findSignalByObjectId(@Param("id") Integer objectId);
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=97234&siteId=1