简体   繁体   中英

Lazy fields always loaded

I have classes with a many-to-one relationship. When I query for them using Criteria.list, the lazy fields are populated.

public class SE {

    @OneToMany(fetch = FetchType.LAZY, mappedBy = "mSe")
    private List<RS> mRs;
}

public class RS {

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "my_join_id")
    private SE mSe;
}

...

Criteria criteria = session.createCriteria(SE.class);    
List<SE> myObjects = criteria.list();

// SE objects have lazy list populated. why?

How do I prevent the lazy fields from being loaded?

How do you know it is populated?

If you are looking with your debugger, it is a normal behavior because the debuger do a lot of stuff and most of the time call the getter to have a string representation of the instance. Since the getter is called, the lazy loading is triggered.

What you can do to ensure it is really lazy loaded is remove your @Transactional annotation, and you should have an error when rying to access SE in the debugger.

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