我有一个存储库
public interface GroupRepository extends JpaRepository<Group, Integer> {
}
组确实有一个列表项
private List<Item> items;
每个项目具有一个 position 属性
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;
}
}
答案 0 :(得分:2)
您可以将查询添加到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);