Hibernate 使用NamedEntityGraph的即时抓取返回的行太多

Hibernate 使用NamedEntityGraph的即时抓取返回的行太多,hibernate,spring-data-jpa,Hibernate,Spring Data Jpa,我拥有以下实体: @Entity @NamedEntityGraph(name = "Text.WithRows", attributeNodes = { @NamedAttributeNode("rows") }) public class Text { @Id @Column(name = "uuid", nullable = false, unique = true) UUID uuid; @Column(name = "belongsTo") UUID belong

我拥有以下实体:

@Entity
@NamedEntityGraph(name = "Text.WithRows", attributeNodes = { @NamedAttributeNode("rows") })
public class Text {
  @Id
  @Column(name = "uuid", nullable = false, unique = true)
  UUID uuid;

  @Column(name = "belongsTo")
  UUID belongsTo;

  @OneToMany
  @JoinColumn(name = "text_id")
  List<TextRow> rows;
}

@Entity
public class TextRow {
  @Id
  @Column(name = "uuid", nullable = false, unique = true)
  private UUID uuid;

  @Column(name = "content", nullable = false, length = 255)
  private String content;
}
@实体
@NamedEntityGraph(name=“Text.WithRows”,attributeNodes={@NamedAttributeNode(“rows”)})
公共类文本{
@身份证
@列(name=“uuid”,nullable=false,unique=true)
UUID-UUID;
@列(name=“belongsTo”)
UUID属于sto;
@独身癖
@JoinColumn(name=“text\u id”)
列出行;
}
@实体
公共类TextRow{
@身份证
@列(name=“uuid”,nullable=false,unique=true)
私有UUID-UUID;
@列(name=“content”,nullable=false,length=255)
私有字符串内容;
}
我还有一个Spring数据JPA存储库,定义如下:

public interface TextRepository extends PagingAndSortingRepository<Text, UUID>, JpaSpecificationExecutor<Text> {
  @EntityGraph(value = "Text.WithRows", type = EntityGraphType.LOAD)
  List<Text> findAllByBelongsTo(UUID belongsTo)
}
public interface TextRepository extends PagingAndSortingRepository<Text, UUID>, JpaSpecificationExecutor<Text> {
  @EntityGraph(value = "Text.WithRows", type = EntityGraphType.LOAD)
  @Query("SELECT DISTINCT txt FROM Text txt WHERE txt.belongsTo = :belongsTo")
  List<Text> findAllByBelongsTo(@Param("belongsTo") UUID belongsTo)
}
公共接口TextRepository扩展了分页和排序存储库,JpaSpecificationExecutor{
@EntityGraph(value=“Text.WithRows”,type=EntityGraphType.LOAD)
列出findAllByBelongsTo(UUID belongsTo)
}
当我从存储库中执行find方法时,我希望立即加载TextRows。因此,我在上面的代码中引入了NamedEntityGraph和EntityGraph注释

我在数据库的文本表中有2个条目,每个条目在TextRow表中有3个条目

我希望findAllByBelongsTo方法返回一个包含两个文本实例的列表。相反,它返回一个包含6个文本实例的列表

我不明白为什么会这样。有人能给我一些指导或解决方案吗


谢谢大家!

好吧,看来我看到的是预期的行为。Hibernate创建一个包含左外部联接的SQL。这将导致父表的n x m结果行

这些问题描述了我的问题并为我提供了解决方案:

简言之,我可以这样注释我的存储库:

public interface TextRepository extends PagingAndSortingRepository<Text, UUID>, JpaSpecificationExecutor<Text> {
  @EntityGraph(value = "Text.WithRows", type = EntityGraphType.LOAD)
  List<Text> findAllByBelongsTo(UUID belongsTo)
}
public interface TextRepository extends PagingAndSortingRepository<Text, UUID>, JpaSpecificationExecutor<Text> {
  @EntityGraph(value = "Text.WithRows", type = EntityGraphType.LOAD)
  @Query("SELECT DISTINCT txt FROM Text txt WHERE txt.belongsTo = :belongsTo")
  List<Text> findAllByBelongsTo(@Param("belongsTo") UUID belongsTo)
}
公共接口TextRepository扩展了分页和排序存储库,JpaSpecificationExecutor{
@EntityGraph(value=“Text.WithRows”,type=EntityGraphType.LOAD)
@查询(“从文本文本中选择不同的文本,其中txt.belongsTo=:belongsTo”)
列出findAllByBelongsTo(@Param(“belongsTo”)UUID belongsTo)
}