如何编写一个自定义的CrudRepository方法(@Query)来过滤我的结果 [英] How to write a custom CrudRepository method(@Query) to filter the result in my case

查看:140
本文介绍了如何编写一个自定义的CrudRepository方法(@Query)来过滤我的结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

private static final long serialVersionUID = 1L;

@Id
字符串ID;

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

字符串描述;

@OneToOne(cascade = {CascadeType.ALL})
类别类别;

....}

类别

  @Entity(name =CATEGORY)
//@NamedQuery(name=\"Category.findByName,query =select 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;

私人字符串名称;

public Category()
{

}

在拍卖仓库中,我添加了一个像这样的方法。

$ $
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);

}

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

c $ c>注释到方法变量名称,以便您可以在查询中引用它。你写的代码非常好。如果您需要访问 EntityManager ,那么您将需要一个定制存储库。

  @Query(from Auction a join a.category c where c.name =:categoryName)
public Iterable< Auction> findByCategory(@Param(categoryName)String categoryName);使用Java时可省略

@Param 8并使用 -parameters 进行编译。



希望有帮助。



提示:每当您发布问题时,也总是发布例外详情。它有助于理解问题。


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;

@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.

解决方案

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<Auction> 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.

这篇关于如何编写一个自定义的CrudRepository方法(@Query)来过滤我的结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆