JPA Criteria API: LEFT JOIN for optional relationships

前端 未结 2 603
滥情空心
滥情空心 2021-02-13 22:13

I\'m using the Criteria API basically the first time. It\'s about abstracting queries for a generic builder:

public TypedQuery newQuery( Manager

        
相关标签:
2条回答
  • 2021-02-13 22:25

    .from(class) does not use an INNER join for all relationships, it only queries the class.

    A relationship will only be queried if you use the join() or fetch() API, to use an outer join use join() with a JoinType.LEFT.

    https://en.wikibooks.org/wiki/Java_Persistence/Criteria#Join

    I'm not sure why you are seeing joins if you are not calling join(). Some JPA providers automatically join fetch all EAGER relationships, this may be what you are seeing. I have always though this odd, perhaps your JPA provider has a away to be configured not to do this, or you can make the relationships LAZY.

    0 讨论(0)
  • 2021-02-13 22:31

    I had the same issue... After investigation the conclusion is That you hâve to handle this with left join in your jpql See that: http://www.objectdb.com/java/jpa/query/jpql/path#Navigation_through_a_NULL_value_

    0 讨论(0)
提交回复
热议问题