Java 在我的情况下如何编写自定义 CrudRepository 方法(@Query)来过滤结果

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30958203/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 10:25:06  来源:igfitidea点击:

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

javamysqlspringhibernatespring-data-jpa

提问by Rajan

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 filter the results by Category.name;

我是 Spring JPA 和 Repository 的新手。我有一个包含一堆字段的 Auction 类,其中一个字段是类别。我想要 CrudRepository 中的自定义方法按 Category.name 过滤结果;

@XmlRootElement 
@Entity(name="AUCTION")
public class Auction implements Serializable{

    private static final long serialVersionUID = 1L;

    @Id
    String id;

    @Column
    @Size(min=5, max=200)
    String title;

    String description;

    @OneToOne(cascade={CascadeType.ALL})
    Category category;

    ....}

Category

类别

@Entity(name="CATEGORY")
//@NamedQuery(name="Category.findByName", query="select c from Category c where c.name=:name")
public class Category implements Serializable{

    private static final long serialVersionUID = 3L;

    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    private Integer id;

    private String name;

    public Category()
    {

    }

In the auction Repository, I added a method like this

在拍卖存储库中,我添加了这样的方法

@Repository
public interface AuctionRepository extends CrudRepository<Auction, String>{

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

}

Its throwing an error. Omit this method and everything works fine. Someone told me that this type of custom methods could be declared in CrudRepository and Spring will take care of doing the right thing using the methodName and the query hint we gave. Could someone point me in the right direction Please.

它抛出一个错误。省略此方法,一切正常。有人告诉我,这种类型的自定义方法可以在 CrudRepository 中声明,Spring 将使用我们提供的 methodName 和查询提示来做正确的事情。有人能指出我正确的方向吗?

采纳答案by Nitin Arora

You need to add @Paramannotation 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.

您需要向@Param方法变量名称添加注释,以便您可以在查询中引用它。你写的代码绝对没问题。如果您需要访问EntityManager,那么您将需要一个自定义存储库。

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

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

@Param使用 Java 8 并使用-parameters.

Hope that helps.

希望有帮助。

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

提示:每当您发布问题时,请务必同时发布例外详情。它有助于理解问题。