【问题标题】:Spring Data+JPA: enforce inner join for OneToOne relationshipSpring Data+JPA:对 OneToOne 关系强制执行内部联接
【发布时间】:2018-08-01 00:50:05
【问题描述】:

我有一个具有 OneToOne 关系的实体,它仅用于对结果进行排序:

@Entity
public class Document {

    @Id
    Long id;

    @OneToOne()
    SortProperty sortProp;

    ...

}

然后我有存储库(使用 QueryDSL 谓词):

public interface DocumentRepository 
             implements PagingAndSortingRepository<Document, Long>,
                        QueryDslPredicateExecutor<Document> {

     @EntityGraph(value = "Document.forceJoins")
     Page<Document> findAll(Predicate queryDslPredicate, Pageable pageable); 

     ...
}

正如您在上面看到的,我使用@EntityGraph 来控制主查询中的连接关系。这一切都很好,唯一的问题是性能 - @OneToOne 是通过 left outer join 获取的,这意味着没有使用 DB 索引:

    select * from
       document document0_ 
    left outer join
       sortproperty sortproper3_ 
        on document0_.documentid=sortproper3_.documentid 
     ... 

有什么方法可以强制使用inner join 而不是left outer join

我已经尝试了几件事 - @OneToOne(optional = false)@org.hibernate.annotations.Fetch,但没有成功...从 QueryDSL 谓词生成的部分正确地使用了属性的内部连接,但查询的主要部分始终使用 left outer join .我也在尝试用这种方法使用注释:

@Query("select doc from Document doc inner join doc.sortProperties props")

但我无法将它与分页和 QueryDSL 谓词一起正确使用。

有什么想法吗?

【问题讨论】:

    标签: postgresql jpa spring-data inner-join querydsl


    【解决方案1】:

    @Query 注释试试这个。

    @Query("select doc from Document doc join doc.sortProp props")
    

    【讨论】:

      猜你喜欢
      • 2021-05-22
      • 2020-01-26
      • 1970-01-01
      • 2019-08-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-07-14
      • 2023-03-12
      相关资源
      最近更新 更多