0

I have @OneToOne relationship between two classes with the following structure:

User:

@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;

}

UserDetail:

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

}

In my userRepository i use an entityGraph to get an left join in the query for userDetail

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

When i call findAll() i expect to have a left join query so hibernate make a query with left join plus a query over user_detail for every row in user table, like follow:

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=?

How to desable query over use_detail ? i have try

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

And when i use a single primary key @JoinColumn instead of @JoinColumnS hibernate make just one query with left join as i except but i need a mutiple keys. the link of the poc here

Thanks for reading me

yobs
  • 11
  • 4

2 Answers2

0

I would try to use attribute optional=false with @OneToOne annotation:

@OneToOne(optional=false)

I solved it in the past and if I remember right it may be correct for your purpose.

stefan-dan
  • 143
  • 13
  • Thank you for your answer, i just try your proposition and i still have two hibernate query :/ – yobs Feb 12 '20 at 21:31
0

This is not as easy possible as it is done with @OneToMany (which is Lazy by default) or @ManyToOne

And a possible Solution would be to have only the Entity which might be null the one-to-one refference and fetch that Entity on demand.

https://stackoverflow.com/a/60108442/10218258 gives a good explenation.