【发布时间】:2013-09-29 01:13:16
【问题描述】:
我想知道是否可以不需要@Query 注释,让 Spring Data 根据我的方法名称和方法参数为以下实体关系构造 JPA 查询。我想检索与特定项目相关的 ItemLocations 列表。我尝试了下面的签名,但没有@Query 它将无法工作。传递 Item.id 而不是 Item 对象本身是否也更合适(高效/有效)?
Spring Data 版本:1.3.4.RELEASE
工作 Spring Data Repository API:
@Query("FROM ItemLocation where item = ?")
public List<ItemLocation> getAllItemLocations(Item item);
所需的 Spring Data Repository API:
public List<ItemLocation> findAllItemLocations(Item item);
JPA 实体:
@Entity
public class ItemLocation {
@ManyToOne
@JoinColumn(name="fk_item_id")
public Item getItem() {
return this.item;
}
public void setItem(Item item) {
this.item = item;
}
@Id
@GeneratedValue
public long getId() {
return this.id;
}
public void setId(long id) {
this.id = id;
}
private Item item;
private long id;
}
@Entity
public class Item {
@OneToMany(mappedBy="item", orphanRemoval=true)
@Cascade({org.hibernate.annotations.CascadeType.PERSIST})
public Set<ItemLocation> getItemLocationList() {
return this.itemLocationList;
}
public void setItemLocationList(Set<ItemLocation> list) {
this.itemLocationList = list;
}
@Id
@GeneratedValue
public long getId() {
return this.id;
}
public void setId(long id) {
this.id = id;
}
private Set<ItemLocation> itemLocationList
= new HashSet<ItemLocation>();
private long id;
}
【问题讨论】:
标签: java jpa spring-data