Here is a simple example of @OneToOne
entity mapping using Java 11, Spring Boot 2.4.5 and Hibernate 5.4.29.
In Java implementation Lazy loading works fine - only Post
entity is loaded:
select id, details_id from post
Here is the same code in Kotlin. It produces following SQL:
select id, details_id from post
select pd.id, p.id, p.details_id
from post_details pd
left outer join post p on pd.id = p.details_id
where pd.id = ?
select p.id, p.details_id from post p
where p.details_id = ?
I do not understand what is wrong here. Mapping is the same but LAZY
is ignored. Moreover the bidirectional association is loaded twice - one SELECT from each side.
SOLUTION UPDATE
In addition to Lukes's solution I would like to point out one Kotlin confusion: kotlin-jpa
plugin is surprisingly not enough to make JPA/Hibernate work properly. We had to configure kotlin-spring
plugin (which is a wrapper for all-open
plugin) and configure it to make @Entity open. This way, Hibernate can finally create proxy allowing lazy loading.