簡體   English   中英

從嵌套列表Spring JpaRepository中檢索對象

[英]Retrieve Object From Nested List Spring JpaRepository

我有一個存儲庫

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

集團確實有一個項目清單

private List<Item> items;

每個物品都有一個位置屬性

private int position;

如何通過了解其中一個列表中的項目位置來檢索

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM