[英]Spring JPA Lazy loading @OneToOne entities doesn't work
I'm having trouble making OrderEntity
object load BillingAddress
lazily.我无法让OrderEntity
object 懒惰地加载BillingAddress
。 I've seen questions revolving around this asked a lot and followed instructions, including adding optional = false but BillingAddress
still gets loaded whenever I findById
an OrderEntity
.我已经看到围绕这个问题提出了很多问题并遵循了说明,包括添加 optional = false 但每当我findById
和OrderEntity
时, BillingAddress
仍然会被加载。
These are my entities (reduced for the sake of this question):这些是我的实体(为了这个问题而减少):
OrderEntity订单实体
@Entity
@Table(name = "orders", schema = "glamitoms")
public class OrderEntity {
@Id
@Column(name = "id")
private int id;
@OneToOne(mappedBy = "order", cascade = CascadeType.ALL, fetch = FetchType.LAZY, optional = false)
private BillingAddressEntity billingAddress;
}
BillingAddressEntity帐单地址实体
@Entity
@Table(name = "billing_address", schema = "glamitoms")
public class BillingAddressEntity {
@Id
@Column(name = "order_id")
private int id;
@OneToOne(fetch = FetchType.LAZY)
@MapsId
private OrderEntity order;
}
TestController测试控制器
@RestController
public class TestController {
private OrdersDAO ordersDAO;
@Autowired
public TestController(OrdersDAO ordersDAO) {
this.ordersDAO = ordersDAO;
}
@GetMapping("/test")
public void test() {
OrderEntity orderEntity = ordersDAO.findById(1).get();
}
}
OrdersDAO订单DAO
@Repository
public interface OrdersDAO extends JpaRepository<OrderEntity, Integer> {
}
The table billing_address
has a FK referencing orders.表billing_address
有一个参考订单的 FK。 I've read contradicting answers saying adding optional = false
should load entities lazily, but for me, that doesn't seem to work.我读过相互矛盾的答案,说添加optional = false
应该懒惰地加载实体,但对我来说,这似乎不起作用。 Am I missing something in any of these entities?我在这些实体中是否缺少某些东西?
Have a look at Vlad Mihalceas article The best way to map a @OneToOne relationship with JPA and Hibernate看看 Vlad Mihalceas 文章The best way to map a @OneToOne relationship with JPA 和 Hibernate
As described there, one of the solutions would be to drop the relation on the parentside...如那里所述,解决方案之一是放弃父母一方的关系......
@Transient
private BillingAddressEntity billingAddress;
and load the BillingAddressEntity
manually using the shared id.并使用共享 ID 手动加载BillingAddressEntity
。
if (order.billingAddress == null)
{
order.billingAddress = entityManager.find(BillingAddressEntity.class, order.id);
}
Another approach would be to drop the shared key, use a foreign key field instead and mark the relation as @ManyToOne
.另一种方法是删除共享密钥,改用外键字段并将关系标记为@ManyToOne
。 This will sacrifice the OneToOne constraint check though.不过,这将牺牲 OneToOne 约束检查。
@ManyToOne(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
@JoinColumn(name = "billing_address_id")
private BillingAddressEntity billingAddress;
Then there's also the byte code enhancement which would allow you to make this a @LazyToOne(LazyToOneOption.NO_PROXY)
relation.然后还有字节码增强功能,它允许您将其@LazyToOne(LazyToOneOption.NO_PROXY)
关系。 I can't help you with that though, as I've never done that myself.不过我帮不了你,因为我自己从来没有这样做过。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.