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()
@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 use 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 like a except but i need a mutiple keys.
the link of the poc here
Thanks for reading me