简体   繁体   中英

How to force Hibernate to use left outer join on a component for a named query?

I try to force Hibernate to use a left outer join for my namedQuery. Unfortunatally it always generates aditionally an inner join, despite me explicitly telling him to use a left outer join.

My named query:

<query name="ZugMultiSelectDTO.findById">
     SELECT DISTINCT NEW ZugMultiSelectDTO(e.id, e.bemerkungZug,e.mergeGesperrt,
                                                e.ankunftsZugDetails.besteller) 
      FROM ZugVersionEntity AS e
      LEFT OUTER JOIN e.ankunftsZugDetails.besteller
      WHERE e.id IN (:id)
</query>

My mapping.xml: ....

<property name="bemerkungZug" column="LOZUV_BEMERK_ZUG" />
<id name="id" column="LOZUV_ID" type="long">
<property name="mergeGesperrt" column="LOZUV_MERGE_GESPERRT" type="int" />
<component name="ankunftsZugDetails" class="...ZugDetails">
    <many-to-one name="besteller" not-found="ignore" lazy="false" cascade="none"  fetch="join">
        <column name="LOZUV_AN_PTEVU_ID_BESTELLER" />
    </many-to-one> 
</component>

Hibernate generated SQL: select distinct zugversion0_.lozuv_id as col_0_0_, zugversion0_.lozuv_bemerk_zug as col_1_0_, zugversion0_.lozuv_merge_gesperrt as col_2_0_, zugversion0_.lozuv_an_ptevu_id_besteller as col_3_0_ from @CISD.TLOZUV zugversion0_ left outer join @CISD.TPTEVU evuentity1_ on zugversion0_.lozuv_an_ptevu_id_besteller=evuentity1_.ptevu_id inner join @CISD.TPTEVU evuentity2_ on zugversion0_.lozuv_an_ptevu_id_besteller=evuentity2_.ptevu_id where zugversion0_.lozuv_id in (411441403011234325L)

AS per http://docs.jboss.org/hibernate/ section 14.3, you could use the following syntax to

> from Cat as cat
>     inner join cat.mate as mate
>     left outer join cat.kittens as kitten
> 
> from Cat as cat left join cat.mate.kittens as kittens
> 
> from Formula form full join form.parameter param

Or you can try the following:

In the hibernate configuration file set the use_outer_join property to true.

Its not possible with components. Hibernate Bug.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM