我有两个具有以下结构的类之间的@OneToOne关系:

用户:

@Getter
@Setter
@NoArgsConstructor
@Entity
public class User implements Serializable {

    @Id
    private Integer id;
    private String login;
    private String password;
    private String name;
    private String address;
    private Boolean active;
    @OneToOne(fetch = FetchType.LAZY)
    @JoinColumns( {@JoinColumn(name = "userDetailId", referencedColumnName="id",  insertable=false, updatable=false),
            @JoinColumn(name = "address", referencedColumnName="location", insertable=false, updatable=false)} )
    private UserDetail userDetail;

}


用户详细信息:

@Getter
@Setter
@NoArgsConstructor
@Entity
public class UserDetail implements Serializable {
    @Id
    private Integer id;
    private String location;

}


在我的userRepository中,我使用entityGraph在userDetail的查询中获得左连接

@Repository
public interface UserRepository extends JpaRepository<User, Integer> {
     @EntityGraph(attributePaths = {"userDetail"})
     List<User> findAll();
}


当我调用findAll()时,我希望有一个左联接查询,因此休眠将使用左联接进行查询,并对用户表中的每一行进行一次user_detail查询,如下所示:

Hibernate:
    select
        user0_.id as id1_0_0_,
        userdetail1_.id as id1_1_1_,
        user0_.active as active2_0_0_,
        user0_.address as address3_0_0_,
        user0_.login as login4_0_0_,
        user0_.name as name5_0_0_,
        user0_.password as password6_0_0_,
        user0_.user_detail_id as user_det7_0_0_,
        userdetail1_.location as location2_1_1_
    from
        user user0_
    left outer join
        user_detail userdetail1_
            on user0_.user_detail_id=userdetail1_.id
            and user0_.address=userdetail1_.location
Hibernate:
    select
        userdetail0_.id as id1_1_0_,
        userdetail0_.location as location2_1_0_
    from
        user_detail userdetail0_
    where
        userdetail0_.id=?
        and userdetail0_.location=?


如何取消对use_detail的查询?
我尝试过

@Fetch(FetchMode.JOIN)
@LazyToOne(LazyToOneOption.NO_PROXY)


当我使用单个主键@JoinColumn而不是@JoinColumnS时,休眠只用一个左联接进行查询,但我需要多个键。
poc here的链接

谢谢你读我

最佳答案

我会尝试在@OneToOne注释中使用attribut optional = true属性:

@OneToOne(optional=false)


我过去曾解决过这个问题,如果我没记错的话,这对您来说可能是正确的。

关于java - @JoinColumnS涉及以一对一关系获取EAGER,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/60196770/

10-10 03:47