Custom queries with CrudRepository

后端 未结 3 1193
夕颜
夕颜 2021-02-04 12:19

I would like to custom my queries with CrudRepository :

This is my code:

@Repository
public interface CustomerRepository extends CrudRepository

        
3条回答
  •  故里飘歌
    2021-02-04 12:53

    Your first problem is in your JPQL:

    @Query("UPDATE customer c SET c.firstName = :firstName WHERE c.id = :id")
    

    type to

    @Query("UPDATE Customer c SET c.firstName = :firstName WHERE c.id = :id")
    

    That is because JPQL wants the query's table name to match your Entity Class name.

    Another problem could be that you are missing the @Modifying annotation:

    @Modifying
    @Query("UPDATE customer c SET c.firstName = :firstName WHERE c.id = :id")
      Integer setNewFirstNameForId(@Param("firstName") String firstName, @Param("id") long id);   
    }
    

    Every time you want modify by query, you should add the @Modifying anotation because Spring needs to know about it.

提交回复
热议问题