Hibernate - avoid unnecessary connections when using a foreign key in that place

I am trying to optimize database queries in Hibernate, but I found a blocker:

<class name="SupportedLanguageVO" table="AR_SUPPORTED_LANG" >
    <cache usage="read-only"/>
 <id name="Id" type="java.math.BigInteger">
  <column name="ID" sql-type="NUMBER(20)" not-null="true"/>
  <generator class="assigned"/>
 </id>
    <property name="OrderSeq" type="java.math.BigInteger">
  <column name="ORDER_SEQ" sql-type="NUMBER(20)" not-null="true"/>
 </property>
    <many-to-one name="Country" class="CountryVO" column="CTRY_CD_ID" cascade="none" >
    <many-to-one name="Language" class="LanguageVO" column="LANG_CD" cascade="none" >

    </class>

The primary key of the country is CTRY_CD_ID. If I fulfill the following criteria

  Criteria crit = m_Session.createCriteria(SupportedLanguageVO.class);
            crit.createCriteria("Country").add(Restrictions.eq("_CountryCode", p_countrycode));
            crit.addOrder(Order.asc("OrderSeq"));

I see hibernation joining the ctry and AR_SUPPORTED_LANG tables. What for? It would be better to run

select * from AR_SUPPORTED_LANG where ctry_cd_id=?

sql instead

select * from AR_SUPPORTED_LANG inner join ctry .... where ctry_cd_id=?

Is it possible to get hibernate to execute the first request?

+5
source share
3 answers

Why? Better run ...

, , . , , . , , , . , .

, . ( HQL):

from AR_SUPPORTED_LANG inner join ctry c where c.cd_id=?

... ...

from AR_SUPPORTED_LANG inner join ctry c WITH c.cd_id=?

WITH HQL AND JOIN.

+1

:

crit.setFetchMode("Country", FetchMode.SELECT);
+1

, . eq :

Criteria crit = m_Session.createCriteria(SupportedLanguageVO.class);
crit.add(Restrictions.eq("Country", p_country));
crit.addOrder(Order.asc("OrderSeq"));

That way, if I remember well, hibernate should optimize the query the way you want. But this means that you need a country object, not just a country code.

+1
source

All Articles