![](/img/trans.png)
[英]Unable to retrieve data from Postgres table using JPArepository and Spring framework
[英]Retrieve Object From Nested List Spring JpaRepository
我有一個存儲庫
public interface GroupRepository extends JpaRepository<Group, Integer> {
}
該集團確實有一個項目清單
private List<Item> items;
每個物品都有一個位置屬性
private int position;
如何通過了解其中一個列表中的項目位置來檢索組 ?
public class Item extends PersistedBean{
private int position;
private Group group;
@Column(name = "Position")
public int getPosition() {
return position;
}
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "GroupId", referencedColumnName = "Id")
public Group getGroup() {
return group;
}
}
public class Group extends PersistedBean {
private int position;
private List<Item> items;
@Column(name = "Position")
public int getPosition() {
return position;
}
@OneToMany(cascade = CascadeType.ALL, mappedBy = "group", orphanRemoval = false)
public List<Item> getItems() {
return items;
}
}
您可以向JpaRepository<Group, Integer>
存儲庫添加查詢,類似應該工作:
@Query("SELECT g FROM Group g " +
"JOIN g.items i ON i.position IN :p")
List<Group> getGroupsWhenPositionMatchAnyRelatedItem(@Param("p") Integer p);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.