Hibernate外键映射?
我有一个实体 A,它具有实体 B 的外键:
entity A --> id, entity_a_name, foreign_key_entity_B
当我调用时,
return session.createCriteria(EntityA.class).list();
我也获得了实体 A 内的实体 B 的属性。如何使其延迟加载,以便在不需要时不会加载 enityB?
I have an entity A that has a foreign key of entity B:
entity A --> id, entity_a_name, foreign_key_entity_B
When I call
return session.createCriteria(EntityA.class).list();
I get the property of entityB inside entity A as well. How do I make it lazy load so it will not load enityB if not needed?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
从您的描述中不清楚您正在谈论什么类型的关系,但如果是多对一或一对一,事情就没那么简单了。如果 A.entityB 可为空(非可选),则 Hibernate 被迫预先加载关系以查看属性是否为空。只有将关系标记为非可选(在这种情况下,Hibernate 假设它不为 null,否则会出现错误),才能使其延迟加载。
It's unclear from your description what type of relationship you are talking about, but if it is Many-to-One or One-to-One, things aren't so straightforward. If
A.entityB
is nullable (non-optional) then Hibernate is forced to eager-load the relationship in order to see if the property is null. Only by marking the relationship as non-optional (in which case Hibernate assumes that it isn't null since it is an error otherwise) can you make it load lazily.