【问题标题】:Hibernate Many-to-one lazy loading is not workingHibernate 多对一延迟加载不起作用
【发布时间】:2012-04-05 16:56:09
【问题描述】:

我有一个类和 2 个多对一映射,我怎样才能延迟加载它们

我的属性

**teneo.mapping.also_map_as_class=true
teneo.mapping.set_proxy=true
teneo.mapping.mapping_file_name=/lazy.hbm.xml**

映射文件(lazy.hbm.xml)

<class name="EnvironmentImpl" entity-name="**Environment**" abstract="false" lazy="true" discriminator-value="Environment" table="`ENVIRONMENT`" proxy="Environment">
    <meta attribute="eclassName">Environment</meta>

    <property name="name" lazy="false" insert="true" update="true" not-null="true" unique="false" type="java.lang.String">
        <column not-null="true" unique="false" name="`NAME`"/>
    </property>

    <property name="description" lazy="false" insert="true" update="true" not-null="false" unique="false" type="java.lang.String">
        <column not-null="false" unique="false" name="`DESCRIPTION`" length="512"/>
    </property>

    <property name="contact" lazy="false" insert="true" update="true" not-null="false" unique="false" type="java.lang.String">
        <column not-null="false" unique="false" name="`CONTACT`"/>
    </property>

    <many-to-one  name="rootAppFolder" entity-name="ApplicationFolder" lazy="proxy" cascade="all" foreign-key="ENVIRONMENT_ROOTAPPFOLDER" insert="true" update="true" not-null="false" unique="true">
        <column not-null="false" unique="false" name="`APPLICATINFLDER_RTAPPFOLDER_ID`"/>
    </many-to-one>

    <many-to-one name="serviceVirtualization" entity-name="ServiceVirtualization" lazy="proxy" cascade="all" foreign-key="ENVIRNMENT_SERVICEVIRTALIZATIN" insert="true" update="true" not-null="false">
        <column not-null="false" unique="false" name="`SRVCVRTLZTN_SRVICVIRTLIZTIN_ID`"/>
    </many-to-one>
</class>

As you can see class Environment has 2 many-to-one's with namesApplicationFolder and serviceVirtualization.

for a HQL query like -  from Environment where name = :name


    Which internally triggers 3 queries  
    Query1 : Environment ( this is right)

    Query2 : ApplicationFolder ( wrong.lazy loading should not.)

    Query 3 : serviceVirtualization ( wrong.lazy loading should not.)



            DEBUG - select environmen0_."NAME" as NAME4_31_, environmen0_."DESCRIPTION" as DESCRIPT5_31_, environmen0_."CONTACT" as CONTACT6_31_, environmen0_."APPLICATINFLDER_RTAPPFOLDER_ID" as APPLICAT7_31_, environmen0_."SRVCVRTLZTN_SRVICVIRTLIZTIN_ID" as SRVCVRTL8_31_ from "ENVIRONMENT" environmen0_ where environmen0_."NAME"=?

            DEBUG - select applicatio0_.e_id as e1_43_0_, applicatio0_.version_ as version3_43_0_, applicatio0_."NAME" as NAME4_43_0_, applicatio0_."ENVIRONMENT_ENV_E_ID" as ENVIRONM5_43_0_ as APPLICAT8_43_0_ from "APPLICATIONFOLDER" applicatio0_ where applicatio0_.e_id=?

            DEBUG - select servicevir0_.e_id as e1_35_0_, servicevir0_.version_ as version3_35_0_, servicevir0_.econtainer_class as econtainer4_35_0_, servicevir0_.e_container as e5_35_0_, servicevir0_.e_container_featureid as e6_35_0_ from "SERVICEVIRTUALIZATION" servicevir0_ where servicevir0_.e_id=?

到目前为止我尝试了哪些方法 1) 设置lazy="no-proxy" ,看不到查询次数有任何变化。

2) 设置 not-found="exception"。

3) 外连接 = "自动"

谁能建议我一个解决方案,以减少由于急切的多对一关系而触发的这 2 个额外查询?

请注意多对一的外键关系。

【问题讨论】:

    标签: hibernate proxy lazy-loading many-to-one


    【解决方案1】:

    lazy 默认为 true。尝试摆脱lazy="proxy"。

    懒惰不工作的原因:

    • 当引用的类型因为没有默认构造函数或最终方法而无法被代理时
    • 引用的类型映射为&lt;class lazy="false"

    【讨论】:

    • 我们不能在多对一上设置lazy="true"。 org.xml.sax.SAXParseException:值为“true”的属性“lazy”必须具有列表“false proxy no-proxy”中的值。我尝试了“无代理”,但急切加载的问题仍然存在。我引用的所有类型都有lazy="true"
    • 您是否在 equals 或 gethashcode 中的某处访问了引用?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-02
    • 1970-01-01
    • 1970-01-01
    • 2013-08-01
    • 2013-02-10
    • 2019-01-18
    相关资源
    最近更新 更多