【发布时间】:2015-09-06 14:54:03
【问题描述】:
我是 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;
....}
类别
@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()
{
}
在拍卖存储库中,我添加了这样的方法
@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);
}
它抛出一个错误。省略此方法,一切正常。有人告诉我,这种类型的自定义方法可以在 CrudRepository 中声明,Spring 会使用我们提供的 methodName 和查询提示来处理正确的事情。请有人指出我正确的方向。
【问题讨论】:
标签: java mysql spring hibernate spring-data-jpa