-->
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.  [ 4 posts ] 
Author Message
 Post subject: Materializing unwanted entities
PostPosted: Tue Feb 10, 2004 8:39 pm 
Newbie

Joined: Wed Dec 10, 2003 3:44 pm
Posts: 12
The following code works perfectly but I can't understand why all the entities included in the composite key (see mapping) are materialized (see log).

Isn't Hibernate able to generate the SQL statement without having to fetch the whole object?

If this is the expected behaviour of the query, does Hibernate supports a different way of querying that prevents this behaviour in those case I know the ids and this is enough to build the query (like in normal SQL)?

Thanks in advance for your attention!

Code:
sql.append("from InstrumentMarketApplication as ia");
sql.append(
    " where ia.comp_id.instrumentMarket.comp_id.instrument.idInstrument = :instrument");
sql.append(" and ia.comp_id.instrumentMarket.comp_id.market.idMarket = :market");
sql.append(" and ia.comp_id.application.idApplication = :application");
sql.append(" and ia.comp_id.instrumentCodeType.idInstrumentCodeType = :codeType");
Query q = session.createQuery(sql.toString());
q.setParameter("instrument", idInstrument,Hibernate.INTEGER);
q.setParameter("market", idMarket, Hibernate.INTEGER);
q.setParameter("application", idApplication, Hibernate.INTEGER);
q.setParameter("codeType", idInstrumentCodeType, Hibernate.INTEGER);
results = q.list();


The log:
Code:
01:07:42,656 DEBUG EntityPersister:417 - Materializing entity: [hibernate.model.Market#39]
01:07:42,671 DEBUG EntityPersister:417 - Materializing entity: [hibernate.model.Instrument#2508]
01:07:43,140 DEBUG EntityPersister:417 - Materializing entity: [hibernate.model.Application#1]
01:07:43,156 DEBUG EntityPersister:417 - Materializing entity: [hibernate.model.InstrumentCodeType#4]
01:07:43,156 DEBUG EntityPersister:417 - Materializing entity: [hibernate.model.InstrumentMarket#hibernate.model.InstrumentMarketPK@1531164[instrument=hibernate.model.Instrument@8ddb93[idInstrument=2508],market=hibernate.model.Market@3aef16[idMarket=39]]]


The mapping file:
Code:
<class
name="hibernate.model.InstrumentMarketApplication"
    table="Instrument_Market_Application"
>
    <composite-id name="comp_id" class="hibernate.model.InstrumentMarketApplicationPK">
        <!-- bi-directional many-to-one association to InstrumentMarket -->
        <key-many-to-one
           name="instrumentMarket"
           class="hibernate.model.InstrumentMarket"
       >
           <column name="Id_Instrument" />
           <column name="Id_Market" />
       </key-many-to-one>
        <!-- bi-directional many-to-one association to Application -->
        <key-many-to-one
           name="application"
           class="hibernate.model.Application"
       >
           <column name="Id_Application" />
       </key-many-to-one>
        <!-- bi-directional many-to-one association to InstrumentCodeType -->
        <key-many-to-one
           name="instrumentCodeType"
           class="hibernate.model.InstrumentCodeType"
       >
           <column name="Id_InstrumentCodeType" />
       </key-many-to-one>
    </composite-id>   


From hibernate.properties:
Code:
hibernate.use_outer_join auto
#hibernate.max_fetch_depth


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 11, 2004 6:30 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
Because composite-id is evil :)

Have a look at that http://www.hibernate.org/Documentation/SomeExplanationsOnLazyLoading

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 11, 2004 2:31 pm 
Newbie

Joined: Wed Dec 10, 2003 3:44 pm
Posts: 12
Thanks for the tip.
The key seems to be the following:

Quote:
So the resume: if your B->C mapping is mandatory (constrainted=true), Hibernate will use proxy for C resulting in lazy initialization. But if you allow B without C, Hibernate just HAS TO check presence of C at the moment it loads B. But a SELECT to check presence is just inefficient because the same SELECT may not just check presence, but load entire object. So lazy loading goes away.


So if I set constrainted=true (because the key mappings are mandatory), Hibernate should not load the key objects, shouldn't it?

Thanks very much for help!


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 12, 2004 7:08 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
If you proxy C, yes

_________________
Emmanuel


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 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.