休眠选择所有查询的执行速度非常慢

问题描述 投票:0回答:1

我有一些简单的代码,应该从mysql数据库中的教师表中选择所有教师对象。当我对其他域对象运行类似的查询时,它的速度非常快,但是由于某种原因,此查询会花费很长时间。另外,如果我从mysql工作台运行全选查询,它会立即执行,所以我认为问题出在我的休眠映射上。表中只有30条ish条目,因此没有理由花费很长时间。

Hibernate映射xml文件:

<hibernate-mapping>
<class name="org.myschool.domain.Teacher" table="teacher">
    <id name="teacher_id" type="java.lang.Integer">
        <column name="teacher_id" />
        <generator class="increment" />
    </id>
    <property name="firstName" type="java.lang.String">
        <column name="first_name" length="20" not-null="true"/>
    </property>
    <property name="lastName" type="java.lang.String">
        <column name="last_name" length="30" not-null="true" />
    </property>
    <property name="middleInitial" type="java.lang.String">
        <column name="middle_initial" length="1" not-null = "false"/>
    </property>

    <set name="wantsCourses" table="teacher_want" inverse="false" lazy="true" fetch="select" cascade="all">
        <key>
            <column name="teacher_id" not-null="true" />
        </key>
        <many-to-many entity-name="org.myschool.domain.Course">
            <column name="course_id" not-null="true" />
        </many-to-many>
    </set>

    <set name="eligibleCourses" table="teacher_eligible" inverse="false" lazy="true" fetch="select" cascade="all">
        <key>
            <column name="teacher_id" not-null="true" />
        </key>
        <many-to-many entity-name="org.myschool.domain.Course">
            <column name="course_id" not-null="true" />
        </many-to-many>
    </set>

    <set name="teacherTranscript" table="assigned_course"  inverse="true" lazy="true" fetch="select">
        <key>
            <column name="teacher_id" not-null="true" />
        </key>

        <one-to-many class="org.myschool.domain.AssignedCourse" />
    </set>

</class>
</hibernate-mapping>

这是要花费很长时间的被调用方法:

public List<Teacher> findAll() {
    try {
        @SuppressWarnings("unchecked")
        List<Teacher> results = (List<Teacher>) sessionFactory.getCurrentSession()
                .createCriteria("org.myschool.domain.Teacher").list();
        log.debug("find all successful, result size: " + results.size());
        return results;
    } catch (RuntimeException re) {
        log.error("find all failed", re);
        throw re;
    }
}

这是正在执行的实际休眠查询:

Hibernate: select this_.teacher_id as teacher_1_8_0_, this_.first_name as first_na2_8_0_, this_.last_name as last_nam3_8_0_, this_.middle_initial as middle_i4_8_0_ from teacher this_

这是制作教师表的sql命令:

CREATE TABLE teacher(
            teacher_id INT(6) UNSIGNED NOT NULL PRIMARY KEY,
            first_name VARCHAR(20) NOT NULL,
            middle_initial CHAR(1),
            last_name VARCHAR(30) NOT NULL)

感谢您的帮助!

java mysql hibernate
1个回答
0
投票

我和您有同样的问题,您找到解决方案了吗?谢谢。

热门问题
推荐问题
最新问题