Hibernate多次运行相同的查询

时间:2016-05-11 10:24:59

标签: sql hibernate lazy-loading

我将show_sql设置为true并运行此查询:

Query q = session.createQuery("select inc FROM Incident inc "
                + "inner join fetch inc.workType "
                + " WHERE inc.incidentId =:id");
q.setLong("id", id);

控制台:

Hibernate: 
select
    incident0_.incident_id as incident1_3_0_,
    worktype1_.work_id as work1_28_1_,
    incident0_.ccs_incident_id as ccs2_3_0_,
    incident0_.process_status as process3_3_0_,
    incident0_.time_created as time4_3_0_,
    incident0_.is_finish as is5_3_0_,
    incident0_.type as type3_0_,
    incident0_.subra_customer_file_id as subra7_3_0_,
    incident0_.description as descript8_3_0_,
    incident0_.group_id as group9_3_0_,
    incident0_.package_size as package10_3_0_,
    incident0_.work_id as work11_3_0_,
    worktype1_.name as name28_1_,
    worktype1_.state_id as state3_28_1_ 
from
    ra.incident incident0_ 
inner join
    ra.work_type worktype1_ 
        on incident0_.work_id=worktype1_.work_id 
where
    incident0_.incident_id=?
Hibernate: 
select
    group0_.group_id as group1_6_1_,
    group0_.name as name6_1_,
    group0_.code as code6_1_,
    group0_.type as type6_1_,
    group0_.is_active as is5_6_1_,
    group0_.group_remote as group6_6_1_,
    group0_.send_data as send7_6_1_,
    employees1_.group_id as group1_3_,
    employee2_.employee_id as employee2_3_,
    employee2_.employee_id as employee1_20_0_,
    employee2_.username as username20_0_,
    employee2_.password as password20_0_,
    employee2_.full_name as full4_20_0_,
    employee2_.ip_address as ip5_20_0_,
    employee2_.last_login as last6_20_0_,
    employee2_.is_locked as is7_20_0_,
    employee2_.ccs_username as ccs8_20_0_,
    employee2_.base64_certificate as base9_20_0_,
    employee2_.is_active as is10_20_0_,
    employee2_.time_created as time11_20_0_,
    employee2_.is_absent as is12_20_0_,
    employee2_.is_system_account as is13_20_0_ 
from
    ra.group group0_ 
left outer join
    group_employee employees1_ 
        on group0_.group_id=employees1_.group_id 
left outer join
    ra.employee employee2_ 
        on employees1_.employee_id=employee2_.employee_id 
where
    group0_.group_id=?
Hibernate: 
select
    workconfig0_.employee_id as employee1_1_,
    workconfig0_.work_config_id as work2_1_,
    workconfig1_.work_config_id as work1_14_0_,
    workconfig1_.work_percent as work2_14_0_,
    workconfig1_.total_work as total3_14_0_,
    workconfig1_.group_id as group4_14_0_,
    workconfig1_.is_template as is5_14_0_,
    workconfig1_.work_type_id as work6_14_0_ 
from
    mn_config workconfig0_ 
left outer join
    ra.work_config workconfig1_ 
        on workconfig0_.work_config_id=workconfig1_.work_config_id 
where
    workconfig0_.employee_id=?
Hibernate: 
select
    group0_.employee_id as employee2_1_,
    group0_.group_id as group1_1_,
    group1_.group_id as group1_6_0_,
    group1_.name as name6_0_,
    group1_.code as code6_0_,
    group1_.type as type6_0_,
    group1_.is_active as is5_6_0_,
    group1_.group_remote as group6_6_0_,
    group1_.send_data as send7_6_0_ 
from
    group_employee group0_ 
left outer join
    ra.group group1_ 
        on group0_.group_id=group1_.group_id 
where
    group0_.employee_id=?

最后两次查询被执行了很多次。我只需要第一个被执行。这是xml文件中的映射关系:

<many-to-one name="group" class="Group"
        column="group_id" fetch="join" unique="true" lazy="proxy"
        not-found="ignore" />

    <set name="certificateRequestHistory" table="certificate_request_history"
        fetch="select" lazy="extra" cascade="delete" >
        <key>
            <column name="incident_id" not-null="true" />
        </key>
        <one-to-many class="CertificateRequestHistory"
            not-found="ignore" />
    </set>

    <set name="businessLogs" table="business_log" inverse="true"
        fetch="select" lazy="true" order-by="time_created desc">
        <key>
            <column name="incident_id" not-null="true" />
        </key>
        <one-to-many class="BusinessLog"
            not-found="ignore" />
    </set>

    <set name="revocationRequestHistory" table="revocation_request_history"
        inverse="true" fetch="select" lazy="true">
        <key>
            <column name="incident_id" not-null="true" />
        </key>
        <one-to-many class="RevocationRequestHistory"
            not-found="ignore" />
    </set>

    <set name="mnProcess" table="mn_process" inverse="true" fetch="select"
        lazy="true">
        <key column="incident_id" not-null="true" />
        <one-to-many class="MnProcess" />
    </set>
    <set name="certificate" table="certificate" inverse="true" fetch="select"
        lazy="extra">
        <key column="incident_id" not-null="true" />
        <one-to-many class="Certificate" />
    </set>
    <set name="unlockRequestHistory" table="unlock_request_history"
        inverse="true" fetch="select" lazy="extra">
        <key column="incident_id" not-null="true" />
        <one-to-many class="UnlockRequestHistory" />
    </set>

    <set name="ccsRequests" table="ccs_request" inverse="true" fetch="select"
        lazy="extra">
        <key column="incident_id" not-null="true" />
        <one-to-many class="CcsRequest" />
    </set>

    <many-to-one name="workType" class="WorkType"
        column="work_id" not-null="false" fetch="join" update="true" insert="true"
        unique="true" lazy="no-proxy" />

我一直在寻找:懒人取,代理等,但没有希望。任何使Hibernate运行这两次的建议?甚至没有?非常感谢!

0 个答案:

没有答案