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