我想知道是否有可能不需要@Query注释,让Spring Data根据我的方法名称和下面的实体关系的方法参数构建JPA查询。我想检索与特定Item相关的ItemLocations列表。我尝试了以下签名,如果没有@Query,它就无法工作。传递Item.id而不是Item对象本身也更合适(高效/有效)吗?
Spring Data Version: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 Enties:
@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;
}
答案 0 :(得分:0)
public List<ItemLocation> findAllByItem(Item item);
(在存储库ItemLocationRepository中)