简体   繁体   中英

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"). I want that this rentables be lazily loaded, but it seems that they are always loaded, even when I use 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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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