【问题标题】:N+1 Hibernate Issue with Bidirectional OneToOne mapping双向 OneToOne 映射的 N+1 休眠问题
【发布时间】:2018-07-04 21:36:27
【问题描述】:

我正在使用 JPARepository 和 hibernate 来表示我的数据库。我有几个实体,我的两个实体有一个双向 OneToOne 映射。每当我对双向关系的子类(或父类以获取子类)运行查询时,我都会遇到 N+1 休眠问题。

我的父实体:

@Entity
@Table(name = "ORGANIZATION")
public class Organization {

    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id", nullable = false)
    @Id
    private Long id;

    @OneToOne(fetch = FetchType.EAGER)
    @JoinColumn(name = "organization_source_id")
    private OrganizationSource source;

    //other columns then getters and setters
}

儿童班

@Entity
@Table(name = "ORGANIZATION_SOURCE")
public class OrganizationSource {

    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id", nullable = false)
    @Id
    private Long id;

    @OneToOne(mappedBy="source", fetch = FetchType.LAZY)
    @Cascade(value=org.hibernate.annotations.CascadeType.SAVE_UPDATE)
    private Organization organization;

    //other columns then getters and setters
}

然后我有我的 DAO 类来创建查询。我使用了@Query 注释

public interface OrganizationSourceDao extends PagingAndSortingRepository<OrganizationSource, Long> {



@Query("SELECT source FROM OrganizationSource source RIGHT JOIN source.organization"
            + "WHERE source.name like %:name% "
    //   order by part of the query
    )
    Page<OrganizationSource> findByNameContaining(
            @Param("name") String name,
            @PageableDefault(size = 10) Pageable pageable);
    }

在 DAO 中执行查询会在日志中显示以下内容

Hibernate: select organizati0_.id as id1_6_, organizati0_.city as city2_6_, organizati0_.code as code3_6_, organizati0_.name as name4_6_, organizati0_.source_system_id as source_s5_6_, organizati0_.state as state6_6_, organizati0_.street as street7_6_, organizati0_.uuid as uuid8_6_, organizati0_.zip as zip9_6_ from organization_source organizati0_ right outer join organization organizati1_ on organizati0_.id=organizati1_.organization_source_id where (organizati0_.name like ?) and (organizati0_.state like ?) and (organizati0_.city like ?) order by case ? when 'ASC' then case ? when 'name' then organizati0_.name when 'state' then organizati0_.state when 'city' then organizati0_.city when 'zip' then organizati0_.zip when 'street' then organizati0_.street end end ASC, case ? when 'DESC' then case ? when 'name' then organizati0_.name when 'state' then organizati0_.state when 'city' then organizati0_.city when 'zip' then organizati0_.zip when 'street' then organizati0_.street end end DESC offset 0 rows fetch next ? rows only
Hibernate: select organizati0_.id as id1_4_1_, organizati0_.mfa_method_id as mfa_meth4_4_1_, organizati0_.organization_source_id as organiza5_4_1_, organizati0_.status_id as status_i6_4_1_, organizati0_.uuid as uuid2_4_1_, organizati0_.version as version3_4_1_, organizati1_.id as id1_6_0_, organizati1_.city as city2_6_0_, organizati1_.code as code3_6_0_, organizati1_.name as name4_6_0_, organizati1_.source_system_id as source_s5_6_0_, organizati1_.state as state6_6_0_, organizati1_.street as street7_6_0_, organizati1_.uuid as uuid8_6_0_, organizati1_.zip as zip9_6_0_ from organization organizati0_ left outer join organization_source organizati1_ on organizati0_.organization_source_id=organizati1_.id where organizati0_.organization_source_id=?
Hibernate: select organizati0_.id as id1_4_1_, organizati0_.mfa_method_id as mfa_meth4_4_1_, organizati0_.organization_source_id as organiza5_4_1_, organizati0_.status_id as status_i6_4_1_, organizati0_.uuid as uuid2_4_1_, organizati0_.version as version3_4_1_, organizati1_.id as id1_6_0_, organizati1_.city as city2_6_0_, organizati1_.code as code3_6_0_, organizati1_.name as name4_6_0_, organizati1_.source_system_id as source_s5_6_0_, organizati1_.state as state6_6_0_, organizati1_.street as street7_6_0_, organizati1_.uuid as uuid8_6_0_, organizati1_.zip as zip9_6_0_ from organization organizati0_ left outer join organization_source organizati1_ on organizati0_.organization_source_id=organizati1_.id where organizati0_.organization_source_id=?
Hibernate: select organizati0_.id as id1_4_1_, organizati0_.mfa_method_id as mfa_meth4_4_1_, organizati0_.organization_source_id as organiza5_4_1_, organizati0_.status_id as status_i6_4_1_, organizati0_.uuid as uuid2_4_1_, organizati0_.version as version3_4_1_, organizati1_.id as id1_6_0_, organizati1_.city as city2_6_0_, organizati1_.code as code3_6_0_, organizati1_.name as name4_6_0_, organizati1_.source_system_id as source_s5_6_0_, organizati1_.state as state6_6_0_, organizati1_.street as street7_6_0_, organizati1_.uuid as uuid8_6_0_, organizati1_.zip as zip9_6_0_ from organization organizati0_ left outer join organization_source organizati1_ on organizati0_.organization_source_id=organizati1_.id where organizati0_.organization_source_id=?
Hibernate: select organizati0_.id as id1_4_1_, organizati0_.mfa_method_id as mfa_meth4_4_1_, organizati0_.organization_source_id as organiza5_4_1_, organizati0_.status_id as status_i6_4_1_, organizati0_.uuid as uuid2_4_1_, organizati0_.version as version3_4_1_, organizati1_.id as id1_6_0_, organizati1_.city as city2_6_0_, organizati1_.code as code3_6_0_, organizati1_.name as name4_6_0_, organizati1_.source_system_id as source_s5_6_0_, organizati1_.state as state6_6_0_, organizati1_.street as street7_6_0_, organizati1_.uuid as uuid8_6_0_, organizati1_.zip as zip9_6_0_ from organization organizati0_ left outer join organization_source organizati1_ on organizati0_.organization_source_id=organizati1_.id where organizati0_.organization_source_id=?
Hibernate: select organizati0_.id as id1_4_1_, organizati0_.mfa_method_id as mfa_meth4_4_1_, organizati0_.organization_source_id as organiza5_4_1_, organizati0_.status_id as status_i6_4_1_, organizati0_.uuid as uuid2_4_1_, organizati0_.version as version3_4_1_, organizati1_.id as id1_6_0_, organizati1_.city as city2_6_0_, organizati1_.code as code3_6_0_, organizati1_.name as name4_6_0_, organizati1_.source_system_id as source_s5_6_0_, organizati1_.state as state6_6_0_, organizati1_.street as street7_6_0_, organizati1_.uuid as uuid8_6_0_, organizati1_.zip as zip9_6_0_ from organization organizati0_ left outer join organization_source organizati1_ on organizati0_.organization_source_id=organizati1_.id where organizati0_.organization_source_id=?
Hibernate: select organizati0_.id as id1_4_1_, organizati0_.mfa_method_id as mfa_meth4_4_1_, organizati0_.organization_source_id as organiza5_4_1_, organizati0_.status_id as status_i6_4_1_, organizati0_.uuid as uuid2_4_1_, organizati0_.version as version3_4_1_, organizati1_.id as id1_6_0_, organizati1_.city as city2_6_0_, organizati1_.code as code3_6_0_, organizati1_.name as name4_6_0_, organizati1_.source_system_id as source_s5_6_0_, organizati1_.state as state6_6_0_, organizati1_.street as street7_6_0_, organizati1_.uuid as uuid8_6_0_, organizati1_.zip as zip9_6_0_ from organization organizati0_ left outer join organization_source organizati1_ on organizati0_.organization_source_id=organizati1_.id where organizati0_.organization_source_id=?

我查看了使用注释 @Fetch(FetchMode.JOIN) 以及所有那些快速解决 Google 上可用的解决方案的方法 - 但似乎无法解决此问题。

任何帮助将不胜感激。

【问题讨论】:

  • RIGHT JOIN FETCH 是要走的路
  • 我尝试这样做时收到以下错误:query specified join fetching, but the owner of the fetched association was not present in the select list [FromElement{explicit,not a collection join,fetch join,fetch non-lazy properties,classAlias=organization,role=OrganizationSource.organization,tableName=organization,tableAlias=organizati1_,origin=organization_source organizati0_,columns={organizati0_.id ,className=Organization}}]
  • 你可以使用 JOIN FETCH 还是 LEFT JOIN FETCH
  • 没有任何形式的 fetch 对我有用 - 即使遵循“正确”的约定

标签: java hibernate jpa spring-data-jpa spring-repositories


【解决方案1】:

我知道一招。将组织的映射从 OneToOne 更改为 ManyToOne。这对我来说一直很好。这次没有 n+1 查询。

组织来源

@Entity
@Table(name = "ORGANIZATION_SOURCE")
public class **OrganizationSource** {

 @GeneratedValue(strategy = GenerationType.IDENTITY)
 @Column(name = "id", nullable = false)
 @Id
 private Long id;

 @JsonBackReference
 @Cascade(value=org.hibernate.annotations.CascadeType.SAVE_UPDATE)
 @ManyToOne(fetch = FetchType.LAZY)
 private Organization organization;

 private String name;
}

和组织

@Data
@Entity
@Table(name = "ORGANIZATION")
public class Organization {

 @GeneratedValue(strategy = GenerationType.IDENTITY)
 @Column(name = "id", nullable = false)
 @Id
 private Long id;

 @JsonManagedReference
 @OneToOne(fetch = FetchType.EAGER)
 @JoinColumn(name = "organization_source_id")
 private OrganizationSource source;

}

【讨论】:

  • 这对我不起作用。当我尝试它时,我得到了一个invalid column name organization_id。我添加了一个@JoinColumn(name="id", updatable="false", insertable="false) 指向 id(因为这是组织表用作外键的内容。这使得使用组织的 id 进行搜索。当我输入“organization_source_id”时,它会说找不到。当我使用 JoinTable 时有效,但 organizationSource 中的组织值为空。
猜你喜欢
  • 2014-12-27
  • 2019-05-02
  • 2012-10-14
  • 1970-01-01
  • 2014-03-12
  • 2017-11-13
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多