小编典典

休眠延迟加载不适用于多对一映射

hibernate

我的多对一映射存在性能问题。当我在日志文件中调试SQL查询时,可以进行主体查询,但是在我有其他表示多对一对象映射的查询之后。

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:

<?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:

<?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:

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;
    }

在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>

阅读 284

收藏
2020-06-20

共1个答案

小编典典

您是否尝试过像这样的FetchMode.SELECT?

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