JPA: Join Fetch results to NULL on empty many side

前端 未结 1 780
陌清茗
陌清茗 2021-01-02 00:36

I have a one to many relationship between User and GameMap. One user can have many maps.

User class:

// LAZY LOADED
@OneToMany(casca         


        
相关标签:
1条回答
  • 2021-01-02 01:01

    A FETCH JOIN actually will resolve to an inner join in SQL. This means that any records/entities in the User table which have no maps will be removed from the result set. You need the LEFT keyword on your FETCH JOIN to get all the results, even those without a map.

    @Query("SELECT u FROM User u LEFT JOIN FETCH u.maps WHERE u.id = (:id)")
    public User findByIdEagerFetch( @Param("id") Long id );
    
    0 讨论(0)
提交回复
热议问题