本文介绍了Hibernate延迟加载不适用于多对一映射的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在多对一映射中遇到了性能问题。当我在日志文件中调试SQL查询时,主查询没问题,但是在我有其他查询代表多对一对象映射之后。

I have problem of performance in my many-to-one mapping. When I debug the SQL query in log file the principal query it's ok, but after i have other query representing many-to-one object mapping.

Entity.hbm.xml :

Entity.hbm.xml :

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >

<hibernate-mapping>
    <class name="com.omb.database.mapping.MyEntity" table="MY_ENTITY">
        <id name="id" type="java.lang.Integer">
            <column name="ENTITY_ID"/>
            <generator class="sequence">
                <param name="sequence">SEQ_MY_ENTITY</param>
            </generator>
        </id>

        <property name="prop1" type="string" column="PROP1" />
        <many-to-one name="object1" column="OBJECT1_ID" class="com.omb.database.mapping.Object1" />
        <many-to-one name="object2" column="OBJECT2_ID" class="com.omb.database.mapping.Object2" /> 

    </class>
</hibernate-mapping>

Object1.hbm.xml:

Object1.hbm.xml :

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >

<hibernate-mapping default-lazy="true">

    <class name="com.omb.database.mapping.Object1" table="TABLE_OBJECT_1">
        <id name="id" type="java.lang.Integer" column="OBJECT1_ID" />
        <property name="label" type="string" column="LABEL_OBJECT_1" length="15" />
    </class>

</hibernate-mapping>

Object2.hbm.xml:

Object2.hbm.xml :

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >

<hibernate-mapping default-lazy="true">

    <class name="com.omb.database.mapping.Object2" table="TABLE_OBJECT_2">
        <id name="id" type="java.lang.Integer" column="OBJECT2_ID" />
        <property name="label" type="string" column="LABEL_OBJECT_2" length="15" />
    </class>

</hibernate-mapping>

查询HBM:

Query HBM :

public List<Entity> findByObject1Id(Integer object1Id) throws DataAccesException {

        List<Entity> results = null;
        try {
            Query query = this.getSession().createQuery(
                    "from Entity ent where ent.object1.id = :object1Id");
            query.setParameter("object1Id", object1Id);
            results = query.list();
        } catch (HibernateException hbe) {
            throw new DataAccesException(hbe);
        }

        return results;
    }

in pom.xml

in pom.xml

<!-- Hibernate 3 -->
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate</artifactId>
            <version>3.2.6.ga</version>
            <exclusions>
                <exclusion>
                    <groupId>javax.transaction</groupId>
                    <artifactId>jta</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>net.sf.ehcache</groupId>
                    <artifactId>ehcache</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>asm</groupId>
                    <artifactId>asm-attrs</artifactId>
                </exclusion>
            </exclusions>
        </dependency>


推荐答案

您是否使用FetchMode.SELECT进行尝试?

Did you try with FetchMode.SELECT, like this;

<many-to-one name="object1" column="OBJECT1_ID" class="com.omb.database.mapping.Object1" fetch="select" />

这篇关于Hibernate延迟加载不适用于多对一映射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-17 12:54