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
}
当我通过ID查询对象时
val deliveryAddress = deliveryAddressService.findById(1)
它是做什么的?
override fun findById(deliveryAddressId: Long): DeliveryAddress? {
return deliveryAddressRepository.findById(deliveryAddressId).orElse(null) // JpaRepository
}
我可以看到执行了以下查询:
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=?
如何使FetchType.LAZY
按预期工作(同时@Basic(fetch = FetchType.LAZY)
对我也不起作用)?
MethodInvocation invocation = new ReflectiveMethodInvocation(proxy, target, method, args, targetClass, chain); retVal = invocation.proceed();
- mleistermethod
在工作。顺便说一下,您可以将属性hibernate.use_sql_comments=true
设置为EntitManagerFactory,也许它会告诉您为什么正在加载关联。另外一件事 - 您可以在getUser()方法或User的每个方法中设置断点,以找出哪些代码正在访问它。 - Stanislav BashkyrtsevDeliveryAddress
类中的toString
/equals
/hashCode
方法是否依赖于user
字段?这可能是触发查询的“隐藏用法”。 - sp00m