【问题标题】:Retrieve Object From Nested List Spring JpaRepository从嵌套列表中检索对象 Spring JpaRepository
【发布时间】:2019-02-16 19:19:19
【问题描述】:

我确实有一个存储库

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

确实有一个列表项目

private List<Item> items;

每个Item都有一个position属性

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

}

【问题讨论】:

  • 你能展示你的实体类吗?
  • 我编辑了问题
  • 所以位置对于所有项目都是唯一的?否则,您将检索具有任何项目的所有组(position = X);
  • 是的,这个职位对所有人都是独一无二的
  • 你试过 findByItemsPositionIn(int pos); 吗?

标签: java nested spring-data-jpa repository jpql


【解决方案1】:

您可以将查询添加到您的 JpaRepository&lt;Group, Integer&gt; 存储库,类似的东西应该可以工作:

@Query("SELECT g FROM Group g " + 
       "JOIN g.items i ON i.position IN :p")
List<Group> getGroupsWhenPositionMatchAnyRelatedItem(@Param("p") Integer p);

【讨论】:

  • 这是 gr8。我会将其标记为已接受。还有一件事。是否可以在此处添加一个可以将购物车指定为参数的 WHERE 语句?谢谢
  • Shure 你可以添加 where 语句,但它是 JPQL 而不是 SQL,记住。
猜你喜欢
  • 2012-10-05
  • 2018-06-08
  • 2020-11-21
  • 2017-11-13
  • 2015-01-19
  • 2017-08-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多