Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
@Entity public class Foo { int v1; int v2; @ManyToOne Bar bar; } Query query = EntityManager.createQuery("select foo from Foo");
上面的查询返回Foo(s),但也加载(s)bar对象。如何防止加载条形对象?
Foo
bar
默认情况下,多对一关系是渴望的。当首选延迟获取时,可以通过以下方式完成:
@ManyToOne (fetch=FetchType.LAZY)
FetchType.LAZY 只是对提供者的提示,根据 JPA 2.0 规范Attributes with FetchType.LAZY may or may not have been loaded。所以不能保证在所有 JPA 实现中都是惰性的——在 Hibernate 中它确实有效。