当前位置: 首页 > 知识库问答 >
问题:

使用@Query和Pageable获取集合对象时连接FETCH不起作用-Spring JPA

林昱
2023-03-14

试图使用可分页的JOIN FETCH获取Post对象。但是它的抛出异常。

实体

@Entity
public class Post {
  @Id
  private String postId;
  private String postName;
  @OneToMany(mappedBy = "Post", cascade = CascadeType.ALL)
  private Set<PostTag> postTags = new HashSet<PostTag>();
}
@Entity
public class Tag {
  @Id
  private String tagId;
  private String tagName;
  @OneToMany(mappedBy = "tag", cascade = CascadeType.ALL)
  @JsonIgnore
  private Set<PostTag> postTags = new HashSet<PostTag>();
}
@Entity
public class PostTag {
  @EmbeddedId
  private PostTagId postTagId = new PostTagId();
  
  @ManyToOne(fetch = FetchType.LAZY)
  @MapsId("postId")
  @JoinColumn(name = "post_Id")
  @JsonIgnore
  private Post post;
  
  @ManyToOne(fetch = FetchType.LAZY)
  @MapsId("tagId")
  @JoinColumn(name = "tag_Id")
  private Tag tag;
  
  @OneToMany(mappedBy = "posttag", cascade = CascadeType.ALL)
  @JsonIgnore
  private Set<Items> items= new HashSet<Items>();

  private String someDateField;
}
@Embeddable
public class PostTagId implements Serializable {
  private String postId;
  private String tagId;
  //equals & hashcode ommited
}
public class Items{
  @Id
  private String itemId;
  private String itemName;

  @ManyToOne(fetch = FetchType.LAZY)
  @JoinColumns({@JoinColumn(name = "post_id"), @JoinColumn(name = "tag_id")})
  @JsonBackReference
  @JsonIgnore
  private PostTag postTag;

  @OneToMany(mappedBy = "items", cascade = CascadeType.ALL)
  @JsonIgnore
  private Set<SubItems> subItems= new HashSet<SubItems>();

}
public class SubItems{
  @Id
  private String subItemId;
  private String subItemName;

  @ManyToOne(fetch = FetchType.LAZY)
  @JoinColumns({@JoinColumn(name = "itemId")})
  @JsonBackReference
  @JsonIgnore
  private Items items;

}

存储库查询如下,

已经尝试过,

@Query(value = "select po from Post po INNER JOIN FETCH po.posttags pts INNER JOIN FETCH pts.tag t INNER JOIN FETCH pts.items i INNER JOIN FETCH i.subitems LEFT OUTER JOIN po.posttags pts2 where (t.tagName like :tName) and (pts.someDateField between :startDate and :endDate) order by pts2.someDateField desc", countQuery = "select count(po) from Post po INNER JOIN po.posttags pts INNER JOIN pts.tag t INNER JOIN pts.items i INNER JOIN i.subitems LEFT OUTER JOIN po.posttags pts2 where (t.tagName like :tName) and (pts.someDateField between :startDate and :endDate) order by pts2.someDateField desc")
Page<Post> findAllInOneQuery(String tName, String startDate, String endDate, Pageable pageable);

但这会导致以下例外。

Caused by: org.hibernate.QueryException: illegal attempt to dereference collection [po0_.postId.postTags] with element property reference [someDateField]

但下面列出的返回类型工作得很好。为什么以及如何使用分页?

@Query(value = "select po from Post po INNER JOIN FETCH po.posttags pts INNER JOIN FETCH pts.tag t INNER JOIN FETCH pts.items i INNER JOIN FETCH i.subitems LEFT OUTER JOIN po.posttags pts2 where (t.tagName like :tName) and (pts.someDateField between :startDate and :endDate) order by pts2.someDateField desc", countQuery = "select count(po) from Post po INNER JOIN po.posttags pts INNER JOIN pts.tag t INNER JOIN pts.items i INNER JOIN i.subitems LEFT OUTER JOIN po.posttags pts2 where (t.tagName like :tName) and (pts.someDateField between :startDate and :endDate) order by pts2.someDateField desc")
    List<Post> findAllInOneQuery(String tName, String startDate, String endDate);

为什么列表按预期工作,但页面对象未按预期工作。是否缺少任何参数,或者是否需要稍微更改查询以适应可分页?

共有1个答案

段干德泽
2023-03-14

我读了你的问题,以下是我的建议:

要将Peagable与查询一起使用,必须为查询指定countBy属性。

@Query(
value = "select po from Post po INNER JOIN FETCH po.posttags pts INNER JOIN FETCH pts.tag t INNER JOIN FETCH pts.items i INNER JOIN FETCH i.subitems LEFT OUTER JOIN po.posttags pts2 where (t.tagName like :tName) and (pts.someDateField between :startDate and :endDate) order by pts2.someDateField desc", countQuery = "select count(po) from Post po INNER JOIN po.posttags pts INNER JOIN pts.tag t INNER JOIN pts.items i INNER JOIN i.subitems LEFT OUTER JOIN po.posttags pts2 where (t.tagName like :tName) and (pts.someDateField between :startDate and :endDate) order by pts2.someDateField desc",
countBy = "select count(po) from Post po INNER JOIN FETCH po.posttags pts INNER JOIN FETCH pts.tag t INNER JOIN FETCH pts.items i INNER JOIN FETCH i.subitems LEFT OUTER JOIN po.posttags pts2 where (t.tagName like :tName) and (pts.someDateField between :startDate and :endDate) order by pts2.someDateField desc", countQuery = "select count(po) from Post po INNER JOIN po.posttags pts INNER JOIN pts.tag t INNER JOIN pts.items i INNER JOIN i.subitems LEFT OUTER JOIN po.posttags pts2 where (t.tagName like :tName) and (pts.someDateField between :startDate and :endDate)")
Page<Post> findAllInOneQuery(String tName, String startDate, String endDate, Pageable pageable);
 类似资料:
  • 使用Fetch Api方法的GetResponse Api集成不起作用 控制台中显示以下SMS: 代码如下: //main.js

  • 我对hibernate和spring-data-jpa还很陌生。我想做的就是使用分页(大约有4000万条记录)来获取一个实体和它的所有相关实体。也就是说,一个查询来急切地获取根实体和它们的所有映射实体/集合=1个查询 我有2个实体类(Customer和Order)如下: 客户实体 尝试使用batchSize,但由于某种原因它无法工作(同样的N+1问题)。 //@Fetch(FetchMode.Se

  • 我使用的是spring-data-jpa。我写了一个本机查询,但它不起作用。这是我的实体类: 以下是查询: 起初,当我没有编写nativeQuery=true时,应用程序没有构建,我得到了一个异常“加入jpa的预期路径”。当我设置nativeQuery=true时,应用程序已启动,但当我调用该函数时,我收到以下错误: 是否有任何其他设置或注释可以解决此问题?我在谷歌上搜索过,但在所有情况下都有两个

  • 我试图通过使用Spring Data中的注释,将排序与集成在联接字段上。 有人建议将添加到参数中,以便在某种程度上与分页(spring data jpa@query和pagable)相对应 我已经学习了Baeldung的教程,但这不包括联接 Spring-Data FETCH JOIN与分页不起作用也建议使用,但我更喜欢使用,而不是. 我将在下面留下一些代码示例。如果我遗漏了一些重要的东西,请随时

  • 我将hibernate-orm与spring-data-jpa一起使用。我有三个实体、、声明如下: 我的目标是获得A和获取b的列表,在实体C上有一些条件,而不获取它。以下JPQL工作正常。 当我尝试使用JPA规范时,我的规范如下所示: 我正在重用由fetch操作完成的隐式连接。此连接不符合规范。它输出以下JPQL。 也就是说,没有别名。 我研究了Hibernate GitHub的源代码。我发现,有

  • 我已经尝试过使用URLSearchParams,但仍然无法使其工作。 谢了。