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

Spring Data JPA @Query with foreign key: Parameter not matched… here is a solution to the problem.

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

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

Object_id is a foreign key. I need to retrieve each signal with a specific object_id.

I’m 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);

}

This will not work. If I change "?1" to 1, it gets hardcoded values. If I try to query “volume”, it works fine.

I’m getting this error :

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

Solution

I suggest omitting the query and letting Spring Data generate one for you. So your case might be represented in some way (if the correct relational mapping is defined):

public interface SignalRepository extends JpaRepository<Signal, Integer> {
    Optional<Signal> findByObject(YourObjectType object);
}

If you provide more information, such as your entity – you can get more help.

Related Problems and Solutions