9

I have the following Model Activity with the language-depending property Title. The language-dependency is defined with two additional entities Translation (Title is of this type, many-to-one) and TranslationValue (one-to-many).

If I write the following hql:

from Activity act join fetch act.Title join fetch act.Title.TranslationValuesSet

This works fine so far. But as soon as I add act into the select-statement, I've got a problem with the join of TranslationValuesSet:

select act from Activity act join fetch act.Title join fetch act.Title.TranslationValuesSet

NHibernate.QueryException: Query specified join fetching, but the owner of the fetched association was not present in the select list [FromElement{explicit,not a collection join,fetch join,fetch non-lazy properties,classAlias=,role=Translation.TranslationValuesSet,tableName=TranslationValue,tableAlias=translatio3_,origin=Translation translatio2_,colums={translatio2_.TranslationId ,className=TranslationValue}}] [select act from Activity act join fetch act.Title join fetch act.Title.TranslationValuesSet

I can't figure out why Hibernate doesn't like that!?

Thx for any tipps!

sl3dg3
  • 5,026
  • 12
  • 50
  • 74

1 Answers1

11

... turns out that I need to define an alias for the first join. This does the trick (notice alias title):

select act from Activity act join fetch act.Title title join fetch title.TranslationValuesSet
sl3dg3
  • 5,026
  • 12
  • 50
  • 74
  • 3
    And why is that? It works for me too, after having problems after upgrading Hibernate version. Did they change something? The exact same query was executed properly in JBoss 4.2.2.GA but not in JBoss 6.0.0.Final. – Sebastian Wramba Sep 06 '11 at 09:59
  • However, this won't work if you try to select a mix of fields and nested entities :( – chrismarx May 12 '16 at 14:19
  • 1
    I faced the same issue when upgrading from Hibernate 3.3 to Hibernate 3.6 and no where in the official migration guide(s) have they even mentioned that Query parser behavior might have changed !! Hibernate needs to do a better job with their migration guides ! – Suketu Bhuta Mar 28 '17 at 21:52
  • You are not supposed to specify an alias for a fetch join, because you're not supposed to use it in the select list. – JL_SO Sep 17 '18 at 17:23