I ve got the following object (simplified):
@Entity
@Table(name = "delivery_addresses")
data class DeliveryAddress (
val street: String
) {
@ManyToOne(fetch=FetchType.LAZY)
lateinit var user: User
}
and when I query the object by ID
val deliveryAddress = deliveryAddressService.findById(1)
which does
override fun findById(deliveryAddressId: Long): DeliveryAddress? {
return deliveryAddressRepository.findById(deliveryAddressId).orElse(null) // JpaRepository
}
I can see the following queries are executed:
select deliveryad0_.street as street6_2_0_, deliveryad0_.user_id as user_id8_2_0_, from delivery_addresses deliveryad0_ where deliveryad0_.id=?
select user0_.id as id1_5_0_, user0_.email as email2_5_0_, user0_.password as password3_5_0_, where user0_.id=?
How can I make FetchType.LAZY
work as excepted (also @Basic(fetch = FetchType.LAZY
) is not working for me)?
MethodInvocation invocation = new ReflectiveMethodInvocation(proxy, target, method, args, targetClass, chain); retVal = invocation.proceed();
method
does the work. BTW, you can set propertyhibernate.use_sql_comments=true
to the EntitManagerFactory, maybe it will tell you why it's loading the association. Another thing - you can set a breakpoint in your getUser() method or in every method of the User to figure out which code accesses it.toString
/equals
/hashCode
ofDeliveryAddress
relying onuser
? Might be a "hidden usage" of the field that triggers the query.