java Hibernate延迟加载
更新时间:2008年10月01日 17:10:44 作者:
对one-to-one 关系进行延迟加载和其他关系相比稍微有些不同。many-to-one 的延迟加载是在配置文件的class 标签
示例数据表:team(班级)、certificate(身份证)、student(学生)
Team.hbm.xml
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!--
Mapping file autogenerated by MyEclipse Persistence Tools
-->
<hibernate-mapping>
<class name="edu.dgut.ke.model.Team" table="TEAM" lazy ="true" ><!-- 多对一的延迟加载设置 -->
<id name="id" type="java.lang.String">
<column name="ID" length="32" />
<generator class="uuid.hex" />
</id>
<property name="teamname" type="java.lang.String">
<column name="TEAMNAME" length="32" not-null="true" unique="true" />
</property>
<set name="students" inverse="true" cascade="all" lazy="true" > <!-- 一对多的延迟加载设置 -->
<key>
<column name="TEAMID" length="32" not-null="true" />
</key>
<one-to-many class="edu.dgut.ke.model.Student" />
</set>
</class>
</hibernate-mapping>
Certificate.hbm.xml
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!--
Mapping file autogenerated by MyEclipse Persistence Tools
-->
<hibernate-mapping>
<class name="edu.dgut.ke.model.Certificate" table="CERTIFICATE" lazy="true" ><!-- 一对一的延迟加载设置 -->
<id name="id" type="java.lang.String">
<column name="ID" length="32" />
<generator class="uuid">
</generator>
</id>
<property name="describe" type="java.lang.String">
<column name="`DESCRIBE`" length="50" not-null="true" />
</property>
<one-to-one name="student"
class="edu.dgut.ke.model.Student"
constrained="true" ><!-- 一对一的延迟加载设置 -->
</one-to-one>
</class>
</hibernate-mapping>
Student.hbm.xml
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!--
Mapping file autogenerated by MyEclipse Persistence Tools
-->
<hibernate-mapping>
<class name="edu.dgut.ke.model.Student" table="STUDENT" lazy="true" >
<id name="id" type="java.lang.String">
<column name="ID" length="32" />
<generator class="uuid.hex" />
</id>
<many-to-one name="certificate"
class="edu.dgut.ke.model.Certificate"
unique="true"
column="cardId"
cascade="all"
>
</many-to-one>
<many-to-one name="team" class="edu.dgut.ke.model.Team">
<column name="TEAMID" length="32" not-null="true" />
</many-to-one>
<property name="studentname" type="java.lang.String">
<column name="STUDENTNAME" length="16" not-null="true" unique="true" />
</property>
</class>
</hibernate-mapping>
注意:对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")。
Team.hbm.xml
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!--
Mapping file autogenerated by MyEclipse Persistence Tools
-->
<hibernate-mapping>
<class name="edu.dgut.ke.model.Team" table="TEAM" lazy ="true" ><!-- 多对一的延迟加载设置 -->
<id name="id" type="java.lang.String">
<column name="ID" length="32" />
<generator class="uuid.hex" />
</id>
<property name="teamname" type="java.lang.String">
<column name="TEAMNAME" length="32" not-null="true" unique="true" />
</property>
<set name="students" inverse="true" cascade="all" lazy="true" > <!-- 一对多的延迟加载设置 -->
<key>
<column name="TEAMID" length="32" not-null="true" />
</key>
<one-to-many class="edu.dgut.ke.model.Student" />
</set>
</class>
</hibernate-mapping>
Certificate.hbm.xml
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!--
Mapping file autogenerated by MyEclipse Persistence Tools
-->
<hibernate-mapping>
<class name="edu.dgut.ke.model.Certificate" table="CERTIFICATE" lazy="true" ><!-- 一对一的延迟加载设置 -->
<id name="id" type="java.lang.String">
<column name="ID" length="32" />
<generator class="uuid">
</generator>
</id>
<property name="describe" type="java.lang.String">
<column name="`DESCRIBE`" length="50" not-null="true" />
</property>
<one-to-one name="student"
class="edu.dgut.ke.model.Student"
constrained="true" ><!-- 一对一的延迟加载设置 -->
</one-to-one>
</class>
</hibernate-mapping>
Student.hbm.xml
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!--
Mapping file autogenerated by MyEclipse Persistence Tools
-->
<hibernate-mapping>
<class name="edu.dgut.ke.model.Student" table="STUDENT" lazy="true" >
<id name="id" type="java.lang.String">
<column name="ID" length="32" />
<generator class="uuid.hex" />
</id>
<many-to-one name="certificate"
class="edu.dgut.ke.model.Certificate"
unique="true"
column="cardId"
cascade="all"
>
</many-to-one>
<many-to-one name="team" class="edu.dgut.ke.model.Team">
<column name="TEAMID" length="32" not-null="true" />
</many-to-one>
<property name="studentname" type="java.lang.String">
<column name="STUDENTNAME" length="16" not-null="true" unique="true" />
</property>
</class>
</hibernate-mapping>
注意:对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")。
相关文章
Java Math类、Random类、System类及BigDecimal类用法示例
这篇文章主要介绍了Java Math类、Random类、System类及BigDecimal类用法,结合实例形式分析了java数值运算相关的Math类、Random类、System类及BigDecimal类基本功能与使用技巧,需要的朋友可以参考下2019-03-03关于feign对x-www-form-urlencode类型的encode和decode问题
这篇文章主要介绍了关于feign对x-www-form-urlencode类型的encode和decode问题,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教2022-03-03浅谈java switch如果case后面没有break,会出现什么情况?
这篇文章主要介绍了浅谈java switch如果case后面没有break,会出现什么情况?具有很好的参考价值,希望对大家有所帮助。一起跟随想小编过来看看吧2020-09-09
最新评论