How to write a custom CrudRepository method(@Query) to filter the result in my case

前端 未结 1 1634
离开以前
离开以前 2021-02-15 18:07

I am new to Spring JPA and Repository stuff. I have an Auction class with a bunch of fields in it, and one of the field is category. I want a custom method in CrudRepository to

1条回答
  •  灰色年华
    2021-02-15 18:48

    You need to add @Param annotation to the method variable name so that you can refer it in your query. Code you have written is absolutely fine. In case you need access to EntityManager, then you will need a custom repository.

    @Query("from Auction a join a.category c where c.name=:categoryName")
    public Iterable findByCategory(@Param("categoryName") String categoryName);
    

    @Param can be omitted when using Java 8 and compiling with -parameters.

    Hope that helps.

    Tip: Whenever you post a question always post the exception details as well. It helps in understanding the issue.

    0 讨论(0)
提交回复
热议问题