简体   繁体   English

JPA:始终加载OneToMany关系(不考虑LAZY)

[英]JPA: OneToMany relationship gets always loaded (LAZY not considered)

I have a JPA entity Rent that has 1:M relationship with the items that are rented (called "Rentable"). 我有一个JPA实体Rent,它与所租借的商品具有1:M的关系(称为“可出租”)。 I want that this rentables be lazily loaded, but it seems that they are always loaded, even when I use fetch = FetchType.LAZY. 我希望此可租项被延迟加载,但即使我使用fetch = FetchType.LAZY,它们也似乎总是被加载。 Here is my code: 这是我的代码:

@Entity
@Table (name = "rent")
public class Rent implements Serializable{

    ........

    @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
    @JoinTable(name = "rent_rentable")
    private List <Rentable> rentables = new ArrayList <Rentable> ();

Here is how I load the rents: 这是我如何收取租金的方法:

 from Rent r  where r.kunde.id = 83

but I also get the associated rentables too. 但是我也得到了相关的租金。 满载租金的租金

Could somebody help me, please? 有人可以帮我吗? I want the rentables to be lazily loaded! 我希望这些可租物品被延迟加载!

Thank you! 谢谢!

When you inspect the collection in the debugger your ORM framework (Hibernate) is forced to lazily load it from the database. 当您在调试器中检查集合时,您的ORM框架(Hibernate)被迫从数据库中延迟加载它。

You need to turn on SQL logging and you will see that your Rentables are only loaded when you either inspect it in the debugger or you call getRentables() on a Rent instance. 您需要打开SQL日志记录,并且会看到,只有在调试器中检查它或在Rent实例上调用getRentables()时,才会加载getRentables()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM