java Hibernate延迟加载

Java教程 2025-08-14

示例数据表:team(班级)、certificate(身份证)、student(学生)

Team.hbm.xml

"http://hibernate.so*urce*fo*rge.net/hibernate-mapping-3.0.dtd">

lazy ="true">

lazy="true" >

Certificate.hbm.xml

"http://hibernate.so*urce*fo*rge.net/hibernate-mapping-3.0.dtd">

lazy="true">

constrained="true">

Student.hbm.xml

"http://hibernate.so*urce*fo*rge.net/hibernate-mapping-3.0.dtd">

unique="true"

column="cardId"

cascade="all"

>

注意:对one-to-one 关系进行延迟加载和其他关系相比稍微有些不同。many-to-one 的延迟加载是在配置文件的class 标签

上设置 lazy="true" ,one-to-many 和 many-to-many 的延迟加载是在 set 标签中设置lazy="true"。而one-to-one 不只要在 classs

标签设置 lazy="true",而且要在one-to-one 标签中设置constrained="true" 。

如果不设置constrained="true",则一对一查询使用默认的预先抓取策略(fetch="join")。