7

After upgrading to a newer hibernate version (guess it came with the switch from JBoss 4.2.2 to JBoss 6), some queries fail with the message:

Caused by: java.lang.IllegalArgumentException: org.hibernate.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=null,role=null,tableName= (...)

This is always the case when using a query like this:

SELECT entityA FROM EntityA entityA 
JOIN FETCH entityA.entityB
LEFT JOIN FETCH entityA.entityB.someField
WHERE entityA.entityB.anotherField LIKE :someParameter

The solution to the problem is to give "entityA.entityB" an alias and then to use this alias in the WHERE clause. But in some queries the LEFT JOIN FETCH is not explicitely given but still the WHERE clause uses the property of a referenced entity. Will it also fail there? What has changed, so that it suddenly fails after switching to a new JBoss version?

The following question is related to this question and includes the solution, but doesn't explain the problem.

Community
  • 1
  • 1
Sebastian Wramba
  • 10,087
  • 8
  • 41
  • 58
  • Your question helped me figure out a solution to my issue, after upgrading from hibernate 3.2.6.ga to 3.5.6-Final, I was getting the 'query specified join fetching, but the owner of the fetched association was not present in the select list', using the alias approach you suggested it works, can't help you with the 'why' though! – Barry Pitman Oct 21 '11 at 11:26
  • is this HQL or standard JPQL? – wrschneider Jun 20 '12 at 17:02
  • This is done via `EntityManager.createQuery`, so I guess it's HQL. – Sebastian Wramba Jun 20 '12 at 20:47
  • @SebastianWramba, 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:46

2 Answers2

3

The query should be

SELECT entityA FROM EntityA entityA 
JOIN FETCH entityA.entityB entityB
LEFT JOIN FETCH entityB.someField
WHERE entityB.anotherField LIKE :someParameter

I.e. you should assign an alias to each joined entity, and use this alias for subsequent joins or restrictions.

JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
  • Yeah I know .. but it's always been this way. And switching from Hibernate 3 to 4 changed the acceptance somehow and this causes a lot of migration work for us. I just wanted to know why they suddenly stopped making Hibernate backwards-compatible on short notice. – Sebastian Wramba Jun 22 '12 at 09:11
  • My guess is that your code worked by accident, by relying on a Hibernate bug that has been fixed now. – JB Nizet Jun 22 '12 at 09:18
0

I have the same trouble in my project and it´s very hard to solve because I have a large legacy system with a lot of modules that use pre-compiled namedqueries and a lot of queries created on demand. To identify and modify the whole system and make changes where was working as well using a old version of hibernate is a very annoying work and error prone. I'm going through this problem in JBoss from 6.4 to 6.4.6 when hibernate version was upgrade from 4.2.18 to 4.2.22 and this error appear. To solve it I make a downgrade of only hibernate main module to initial default version 4.2.18 but this is not good because when the next patch of JBoss arrive I need to change it again. I´m trying use the JBoss modules and some configuration in persistence.xml and jboss-deployment-strcuture.xml but without success yet.

Carlos Lacerda
  • 699
  • 6
  • 14