如何在hibernate中实现延迟加载

时间:2010-03-03 10:09:36

标签: hibernate

我有3个带映射的表 公司注册                                                 

    <one-to-one name="referenceDb"   class="com.hibermappings.ReferenceDb"  cascade="all" constrained="false" /> 
    <one-to-one name="registration"  class="com.hibermappings.Registration" cascade="all" constrained="false" /> 

    <property name="companyName" type="java.lang.String">
        <column name="companyName" length="50" />
    </property>
    <property name="companyProfile" type="java.lang.String">
        <column name="companyProfile" length="50" />
    </property>
    <!--<property name="functionalArea" type="java.lang.String">
        <column name="functionalArea" length="50" />
    </property>
    --><property name="contactPerson" type="java.lang.String">
        <column name="contactPerson" length="50" />
    </property>
    <property name="website" type="java.lang.String">
        <column name="website" length="50" />
    </property>
    <property name="country" type="java.lang.String">
        <column name="country" length="50" />
    </property>
    <property name="state" type="java.lang.String">
        <column name="state" length="50" />
    </property>
    <property name="city" type="java.lang.String">
        <column name="city" length="50" />
    </property>
    <property name="address" type="java.lang.String">
        <column name="address" length="50" />
    </property>
    <property name="email" type="java.lang.String">
        <column name="email" length="50" />
    </property>
    <property name="mobile" type="java.lang.String">
        <column name="mobile" length="50" />
    </property>
    <property name="phone" type="java.lang.String">
        <column name="phone" length="50" />
    </property>
    <property name="fax" type="java.lang.String">
        <column name="fax" length="50" />
    </property>
    <property name="image" type="java.lang.String">
        <column name="image" length="50" />
    </property>
    <property name="video" type="java.lang.String">
        <column name="video" length="50" />
    </property>
</class>

第二个是Referencedb

                                                                 currentprofile

            </generator>
    </id>
    <one-to-one name="currentprofile" class="com.hibermappings.Currentprofile"  constrained="false" />

    <property name="referencedId" type="string">
        <column name="referenced_id" length="30" />
    </property>
    <property name="password" type="string">
        <column name="password" length="10" />
    </property>
    <property name="associateCode" type="string">
        <column name="associate_code" length="8" />
    </property>
    <property name="active" type="string">
        <column name="active" length="1" />
    </property>
    <property name="userType" type="string">
        <column name="user_type" length="50" />
    </property>
    <property name="numericId" type="integer">
        <column name="numeric_id" />
    </property>
</class>

,第三是注册                                                              currentprofile
                                  

    <property name="name" type="java.lang.String">
        <column name="name" length="80" />
    </property>
    <property name="joinDate" type="java.lang.String">
        <column name="join_date" length="15" />
    </property>
    <property name="orgname" type="java.lang.String">
        <column name="orgname" length="80" />
    </property>
    <property name="cperson" type="java.lang.String">
        <column name="cperson" length="80" />
    </property>
    <property name="mobile" type="java.lang.String">
        <column name="mobile" length="15" />
    </property>
    <property name="phone1" type="java.lang.String">
        <column name="phone1" length="20" />
    </property>
    <property name="phone2" type="java.lang.String">
        <column name="phone2" length="20" />
    </property>
    <property name="fax" type="java.lang.String">
        <column name="fax" length="20" />
    </property>
    <property name="address" type="java.lang.String">
        <column name="address" length="65535" />
    </property>
    <property name="country" type="java.lang.String">
        <column name="country" length="50" />
    </property>
    <property name="state" type="java.lang.String">
        <column name="state" length="50" />
    </property>
    <property name="city" type="java.lang.String">
        <column name="city" length="50" />
    </property>
    <property name="pincode" type="java.lang.String">
        <column name="pincode" length="6" />
    </property>
    <property name="email" type="java.lang.String">
        <column name="email" length="80" />
    </property>
    <property name="associateLocation" type="java.lang.String">
        <column name="associate_location" length="50" />
    </property>
    <property name="designation" type="java.lang.String">
        <column name="designation" length="50" />
    </property>
    <property name="chatType" type="java.lang.String">
        <column name="chatType" length="30" />
    </property>
    <property name="gmailChatId" type="java.lang.String">
        <column name="gmailChatId" length="50" />
    </property>
    <property name="hotmailChatId" type="java.lang.String">
        <column name="hotmailChatId" length="50" />
    </property>
    <property name="rediffchatId" type="java.lang.String">
        <column name="rediffchatId" length="50" />
    </property>
    <property name="yahoochatId" type="java.lang.String">
        <column name="yahoochatId" length="50" />
    </property>
    <property name="otherChatId" type="java.lang.String">
        <column name="otherChatId" length="50" />
    </property>
    <property name="activateDate" type="java.util.Date">
        <column name="activate_date" length="0" />
    </property>
    <property name="fname" type="java.lang.String">
        <column name="fName" length="200" />
    </property>
    <property name="perAddress" type="java.lang.String">
        <column name="perAddress" length="50" />
    </property>
    <property name="prefereduserid" type="java.lang.String">
        <column name="prefereduserid" length="50"  />
    </property>
    <property name="useby" type="java.lang.String">
        <column name="useby" length="2" />
    </property>
    <property name="academytype" type="java.lang.String">
        <column name="academytype" length="75" />
    </property>
</class>

现在,当我尝试使用companyId从CompanyRegistrtion表加载对象时,还会加载Referencedb和Registration的子对象,最后 我得到了例外.. 我怎样才能补充延迟加载。

1 个答案:

答案 0 :(得分:2)

Hibernate默认使用延迟加载。但是,它在您的映射中被明确禁用:

<one-to-one name="referenceDb" lazy="false" ... />
<one-to-one name="registration" lazy="false" ... />

删除lazy="false"可以解决您的问题。