I've got the following entity-relations (in Kotlin):
InvoiceEntity:
@OneToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "contact")
var contactEntity: ContactEntity?,
ContactEntity:
@OneToOne(mappedBy = "contactEntity", fetch = FetchType.EAGER)
var accountingContactEntity: AccountingContactEntity?
AccountingContactEntity:
@OneToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "contact")
var contactEntity: ContactEntity?
When I execute the Query-Method
@EntityGraph(
attributePaths = ["contactEntity"],
type = EntityGraph.EntityGraphType.LOAD
)
findByDateBetween(...): Set<InvoiceEntity>
then I expect that the query contains a JOIN
for the Contact-Entity and the AccountingContactEntity.
But it is missing for the AccountingContactEntity. Instead, an extra SELECT
is excecuted for each AccountingContactEntity (N+1). Shouldn't EntityGraph.EntityGraphType.LOAD
trigger the recognition of the FetchType.EAGER Annotation in the ContactEntity, forcing a JOIN
for the AccountingContactEntity?
The only thing that works is adding "contactEntity.accountingContactEntity"
to the EntityGraph.
Isn't there another solution?
By the way, if I'm setting @OneToOne(fetch = FetchType.EAGER)
for the contactEntity of the InvoiceEntity and excecute a normal find(invoiceId)
, then both tables get joined as expected.