hibernate/hibernate-orm · error · SemanticException

All query parts in a query group must have the same join fet

Error message

All query parts in a query group must have the same join fetches in the same order

What it means

Error "All query parts in a query group must have the same join fetches in the same order" thrown in hibernate/hibernate-orm.

Source

Thrown at hibernate-core/src/main/java/org/hibernate/query/sqm/tree/spi/select/SqmQueryGroup.java:196

					}
				}
			}
			else {
				throw new AssertionFailure( "Unrecognized SqmQueryPart subtype" );
			}
		}
	}

	private void validateFetchesMatch(SqmFrom<?, ?> firstFrom, SqmFrom<?, ?> from) {
		final var firstJoinIter = firstFrom.getSqmJoins().iterator();
		final var joinIter = from.getSqmJoins().iterator();
		while ( firstJoinIter.hasNext() ) {
			final SqmJoin<?, ?> firstSqmJoin = firstJoinIter.next();
			if ( firstSqmJoin instanceof SqmAttributeJoin<?, ?> firstAttrJoin ) {
				if ( firstAttrJoin.isFetched() ) {
					final var matchingAttrJoin = findFirstFetchJoin( joinIter );
					if ( matchingAttrJoin == null || firstAttrJoin.getModel() != matchingAttrJoin.getModel() ) {
						throw new SemanticException(
								"All query parts in a query group must have the same join fetches in the same order"
						);
					}
					validateFetchesMatch( firstAttrJoin, matchingAttrJoin );
				}
			}
		}
		// At this point, the other iterator should only contain non-fetch joins
		while ( joinIter.hasNext() ) {
			if ( joinIter.next() instanceof SqmAttributeJoin<?, ?> attrJoin
					&& attrJoin.isFetched() ) {
				throw new SemanticException(
						"All query parts in a query group must have the same join fetches in the same order"
				);
			}
		}
	}

View on GitHub (pinned to fad1729dce)

Solutions

  1. Give every query part in the group the same join fetches, declared in the same order.
  2. Remove the join fetch from the parts that do not have it, or add the identical fetch to all parts.
  3. Move the fetch out of the grouped queries if it is only needed on one part, and fetch it in a separate query.

When it happens

Trigger: Query parts joined by set operations declare join fetches that differ in content or order.

Common situations: Adding a fetch join in only one UNION branch; mirror the same fetches in the same order in every part.


AI-assisted analysis of hibernate/hibernate-orm@fad1729dce (2026-08-22). Data as JSON: /api/errors/a58d3acaaf6e6fa6. Report an issue: GitHub.