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.  [ 1 post ] 
Author Message
 Post subject: Hibernate.initialize or session.find?
PostPosted: Wed Feb 18, 2004 7:25 am 
Pro
Pro

Joined: Wed Oct 08, 2003 10:31 am
Posts: 247
-----------
Model
-----------
Code:
mp_req_item (PK: mp_requisicao_fk, mp_fk)
|
|
|----------> mp (PK: id)
|
|----------> mp_requisicao_sq044 (PK: id)


--------------------------------------------------------
Mapping files (generated with Middlegen R3):
--------------------------------------------------------

MpRequisicaoSq044
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
    "-//Hibernate/Hibernate Mapping DTD 2.0//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd" >
   
<hibernate-mapping>
<!--
    Created by Middlegen Hibernate plugin

    http://boss.bekk.no/boss/middlegen/
    http://hibernate.sourceforge.net/
-->

<class
    name="vo.MpRequisicaoSq044"
    table="mp_requisicao_sq044"
>

    <id
        name="id"
        type="long"
        column="id"
    >
        <generator class="increment" />
    </id>

    <property
        name="dataRequisicao"
        type="java.sql.Date"
        column="data_requisicao"
        not-null="true"
        length="4"
    />
    <property
        name="estado"
        type="java.lang.String"
        column="estado"
        length="1"
    />
    <property
        name="destino"
        type="java.lang.String"
        column="destino"
        length="15"
    />
    <property
        name="userId"
        type="java.lang.String"
        column="user_id"
        not-null="true"
        length="10"
    />
    <property
        name="dtHr"
        type="java.sql.Timestamp"
        column="dt_hr"
        not-null="true"
        length="8"
    />

    <!-- associations -->
    <!-- bi-directional one-to-many association to MpReqItem -->
    <set
        name="mpReqItems"
        lazy="true"
        inverse="true"
    >
        <key>
            <column name="mp_requisicao_fk" />
        </key>
        <one-to-many
            class="vo.MpReqItem"
        />
    </set>

</class>
</hibernate-mapping>


Mp
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
    "-//Hibernate/Hibernate Mapping DTD 2.0//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd" >
   
<hibernate-mapping>
<!--
    Created by Middlegen Hibernate plugin

    http://boss.bekk.no/boss/middlegen/
    http://hibernate.sourceforge.net/
-->

<class
    name="vo.Mp"
    table="mp"
>

    <id
        name="id"
        type="long"
        column="id"
    >
        <generator class="increment" />
    </id>

    <property
        name="referencia"
        type="java.lang.String"
        column="referencia"
        not-null="true"
        length="-1"
    />
    <property
        name="designacao"
        type="java.lang.String"
        column="designacao"
        not-null="true"
        length="-1"
    />
    <property
        name="modelo"
        type="java.lang.String"
        column="modelo"
        length="-1"
    />
    <property
        name="tipo"
        type="java.lang.String"
        column="tipo"
        length="-1"
    />
    <property
        name="peso"
        type="java.math.BigDecimal"
        column="peso"
        length="10"
    />
    <property
        name="fornecedorFk"
        type="java.lang.String"
        column="fornecedor_fk"
        not-null="true"
        length="-1"
    />
    <property
        name="dataMonitorizacao"
        type="java.sql.Date"
        column="data_monitorizacao"
        length="4"
    />
    <property
        name="desenhoMaterial"
        type="java.lang.String"
        column="desenho_material"
        length="-1"
    />
    <property
        name="norma"
        type="java.lang.String"
        column="norma"
        length="-1"
    />
    <property
        name="legislacao"
        type="java.lang.String"
        column="legislacao"
        length="-1"
    />
    <property
        name="fabricante"
        type="java.lang.String"
        column="fabricante"
        length="-1"
    />
    <property
        name="estado"
        type="java.lang.String"
        column="estado"
        length="1"
    />
    <property
        name="estadoMonit"
        type="java.lang.String"
        column="estado_monit"
        length="-1"
    />
    <property
        name="userId"
        type="java.lang.String"
        column="user_id"
        not-null="true"
        length="10"
    />
    <property
        name="dtHr"
        type="java.sql.Timestamp"
        column="dt_hr"
        not-null="true"
        length="8"
    />

    <!-- associations -->
    <!-- bi-directional one-to-many association to MpReqItem -->
    <set
        name="mpReqItems"
        lazy="true"
        inverse="true"
    >
        <key>
            <column name="mp_fk" />
        </key>
        <one-to-many
            class="vo.MpReqItem"
        />
    </set>
</class>
</hibernate-mapping>


MpReqItem
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
    "-//Hibernate/Hibernate Mapping DTD 2.0//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd" >
   
<hibernate-mapping>
<!--
    Created by Middlegen Hibernate plugin

    http://boss.bekk.no/boss/middlegen/
    http://hibernate.sourceforge.net/
-->

<class
    name="vo.MpReqItem"
    table="mp_req_item"
>

    <composite-id name="comp_id" class="vo.MpReqItemPK">
        <!-- bi-directional many-to-one association to MpRequisicaoSq044 -->
        <key-many-to-one
           name="mpRequisicaoSq044"
           class="vo.MpRequisicaoSq044"
       >
           <column name="mp_requisicao_fk" />
       </key-many-to-one>
        <!-- bi-directional many-to-one association to Mp -->
        <key-many-to-one
           name="mp"
           class="vo.Mp"
       >
           <column name="mp_fk" />
       </key-many-to-one>
    </composite-id>   

    <property
        name="quantidade"
        type="int"
        column="quantidade"
        length="4"
    />
    <property
        name="userId"
        type="java.lang.String"
        column="user_id"
        not-null="true"
        length="10"
    />
    <property
        name="dtHr"
        type="java.sql.Timestamp"
        column="dt_hr"
        not-null="true"
        length="8"
    />

    <!-- associations -->

</class>
</hibernate-mapping>


-----------------------------------------------
Generated SQL code in each situation
-----------------------------------------------
1) With:
Code:
MpRequisicaoSq044 mp_req = (MpRequisicaoSq044)session.get(MpRequisicaoSq044.class, mp_req_id);
Hibernate.initialize(mp_req.getMpReqItems());


I get:
Code:
select mprequisic0_.id as id0_,
mprequisic0_.data_requisicao as data_req2_0_,
mprequisic0_.estado as estado0_,
mprequisic0_.destino as destino0_,
mprequisic0_.user_id as user_id0_,
mprequisic0_.dt_hr as dt_hr0_
from mp_requisicao_sq044 mprequisic0_ where mprequisic0_.id=?

select mpreqitems0_.mp_requisicao_fk as mp_requi1___,
mpreqitems0_.mp_fk as mp_fk__,
mpreqitems0_.mp_requisicao_fk as mp_requi1_0_,
mpreqitems0_.mp_fk as mp_fk0_,
mpreqitems0_.quantidade as quantidade0_,
mpreqitems0_.user_id as user_id0_,
mpreqitems0_.dt_hr as dt_hr0_
from mp_req_item mpreqitems0_ where mpreqitems0_.mp_requisicao_fk=?

select mp0_.id as id0_,
mp0_.referencia as referencia0_,
mp0_.designacao as designacao0_,
mp0_.modelo as modelo0_,
mp0_.tipo as tipo0_,
mp0_.peso as peso0_,
mp0_.fornecedor_fk as forneced7_0_,
mp0_.data_monitorizacao as data_mon8_0_,
mp0_.desenho_material as desenho_9_0_,
mp0_.norma as norma0_,
mp0_.legislacao as legislacao0_,
mp0_.fabricante as fabricante0_,
mp0_.estado as estado0_,
mp0_.estado_monit as estado_14_0_,
mp0_.user_id as user_id0_,
mp0_.dt_hr as dt_hr0_
from mp mp0_ where mp0_.id=?


2) With:
Code:
List res = session.find("from vo.MpRequisicaoSq044 mr join fetch mr.mpReqItems where mr.id = ?", mp_req_id, Hibernate.LONG);


I get:
Code:
select mprequisic0_.id as id0_,
mpreqitems1_.mp_requisicao_fk as mp_requi1_1_,
mpreqitems1_.mp_fk as mp_fk1_,
mprequisic0_.data_requisicao as data_req2_0_,
mprequisic0_.estado as estado0_,
mprequisic0_.destino as destino0_,
mprequisic0_.user_id as user_id0_,
mprequisic0_.dt_hr as dt_hr0_,
mpreqitems1_.quantidade as quantidade1_,
mpreqitems1_.user_id as user_id1_,
mpreqitems1_.dt_hr as dt_hr1_,
mpreqitems1_.mp_requisicao_fk as mp_requi1___,  (1)
mpreqitems1_.mp_fk as mp_fk__  (2)
from mp_requisicao_sq044 mprequisic0_ inner join mp_req_item mpreqitems1_ on mprequisic0_.id=mpreqitems1_.mp_requisicao_fk where (mprequisic0_.id=? )

select mprequisic0_.id as id0_,
mprequisic0_.data_requisicao as data_req2_0_,
mprequisic0_.estado as estado0_,
mprequisic0_.destino as destino0_,
mprequisic0_.user_id as user_id0_,
mprequisic0_.dt_hr as dt_hr0_
from mp_requisicao_sq044 mprequisic0_ where mprequisic0_.id=?

select mp0_.id as id0_,
mp0_.referencia as referencia0_,
mp0_.designacao as designacao0_,
mp0_.modelo as modelo0_,
mp0_.tipo as tipo0_,
mp0_.peso as peso0_,
mp0_.fornecedor_fk as forneced7_0_,
mp0_.data_monitorizacao as data_mon8_0_,
mp0_.desenho_material as desenho_9_0_,
mp0_.norma as norma0_,
mp0_.legislacao as legislacao0_,
mp0_.fabricante as fabricante0_,
mp0_.estado as estado0_,
mp0_.estado_monit as estado_14_0_,
mp0_.user_id as user_id0_,
mp0_.dt_hr as dt_hr0_
from mp mp0_ where mp0_.id=?


--------------
Questions
--------------
1) Is "Hibernate.initialize()" more eficiente than "session.find()" when we know the ID? From the above generated SQL it looks like it.

2) In the second situation, why does the (1) and (2) always appear in a "fetch" situation, regardless of the mapping files involved? There're not used and there's already alias for them in the query.


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

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.