从嵌套列表 Spring JpaRepository 中检索对象

Retrieve Object From Nested List Spring JpaRepository

我有一个存储库

public interface GroupRepository extends JpaRepository<Group, Integer> {
}

确实有一个列表

private List<Item> items;

每个 Item 都有一个 position 属性

private int position;

How can I retrieve a group by knowing the position of an item present in one of the lists?

Item.java

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;
  }
}

Group.java

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);