2

I am using Hibernate in a project where we have bout 300 entities.

For many entities relation we need no lazy loading, so we have used default-lazy=false in the mapping, and where we need lazy loading, we have used lazy=true for collections and lazy=proxy for many to one relation.

lazy=true works fine for collections, but lazy=proxy is ignored in many-to-one case. When I query Vehicle entity which have many-to-one mapping with the User entity, Hibernate loads User entity as well, i.e ignoring lazy=proxy. Below is the short version of mapping file.

<hibernate-mapping package="com.test.bean" default-lazy="false">
<class name="Vehicle" table="tbl_vehicle" optimistic-lock="version" >
    <id name="vehicleNo" column="VEHICLE_NO" type="int" node="@id">
        <generator class="com.test.TestSequenceGenerator" />
    </id>
    <version name="version" column="version" type="integer" unsaved-value="undefined" generated="never"/>
    <property name="color" />
    <property name="wheel" />
    <property name="seat" />

    <many-to-one name="user" class="User" embed-xml="false" lazy="proxy" fetch="select">
        <column name="user_id" not-null="true" />
    </many-to-one>
</class>

<class name="User" table="tbl_user" optimistic-lock="version" >
    <id name="userId" column="user_id" type="int" node="@id">
        <generator class="com.test.TestSequenceGenerator" />
    </id>
    <version name="version" column="version" type="integer" unsaved-value="undefined" generated="never"/>

    <property name="name" />

    <set name="vehicles" table="tbl_vehicle" cascade="all"
            inverse="true" fetch="select" lazy="true">
              <key>
                  <column name="user_id" not-null="true" />
              </key>
              <one-to-many class="Vehicle" />
           </set>

</class>
</hibernate-mapping>
4

1 回答 1

0

我同意你们的看法。然而,在这里,类映射 User 不应该有一个 default-lazy="false" 来让一个lazy="proxy" 工作。

于 2014-01-28T13:13:00.023 回答