-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: Hibernate run the same queries many times
PostPosted: Wed May 11, 2016 11:39 pm 
Newbie

Joined: Wed May 11, 2016 11:35 pm
Posts: 1
I set show_sql to true and run this query:
Code:
Query q = session.createQuery("select inc FROM Incident inc "
                + "inner join fetch inc.workType "
                + " WHERE inc.incidentId =:id");
q.setLong("id", id);


The console:

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


The last two queries are executed many more times. I just need the first one to be executed. This is the mapping relation in the xml file:
Code:
<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" />


I have been searching: lazy fetching, proxy, etc but no hope. Any suggestion to make Hibernate run those two once? Or even none? Much appreciated!


Top
 Profile  
 
 Post subject: Re: Hibernate run the same queries many times
PostPosted: Mon May 16, 2016 7:57 am 
Hibernate Team
Hibernate Team

Joined: Thu Sep 11, 2014 2:50 am
Posts: 1628
Location: Romania
The mapping is missing the employee and the group_employee mappings.
I suggest you switch from EAGER fetching to LAZY, and then use JOIN FETCH for the associations that you need to fetch on a per query basis. This way, you have better control on what is to be fetched.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.