简体   繁体   English

懒惰的字段总是加载

[英]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. 当我使用Criteria.list查询它们时,将填充惰性字段。

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. 如果您正在使用调试器,这是一种正常行为,因为debuger会执行很多操作,并且大多数时候会调用getter来获取实例的字符串表示形式。 Since the getter is called, the lazy loading is triggered. 由于调用了getter,因此会触发延迟加载。

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. 你可以做些什么来确保它实际上是延迟加载的是删除你的@Transactional注释,当你在调试器中访问SE时你应该有一个错误。

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

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