亲宝软件园·资讯

展开

Hibernate延迟 java Hibernate延迟加载

人气:0
想了解java Hibernate延迟加载的相关内容吗,在本文为您仔细讲解Hibernate延迟的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:java,Hibernate,延迟,下面大家一起来学习吧。
示例数据表:team(班级)、certificate(SFZ)、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")。

加载全部内容

相关教程
猜你喜欢
用户评论