Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"Automatic" join fetching of nested entities fails after updating Hibernate

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.

like image 523
Sebastian Wramba Avatar asked Jul 08 '11 12:07

Sebastian Wramba


1 Answers

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.

like image 85
JB Nizet Avatar answered Nov 10 '22 06:11

JB Nizet