Skip to content

Commit

Permalink
HHH-14482 Do not discard prior implicit join by key
Browse files Browse the repository at this point in the history
  • Loading branch information
dreab8 committed Mar 10, 2021
1 parent a022127 commit 1905e8b
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 5 deletions.
Expand Up @@ -45,6 +45,12 @@ public SemanticPathPart resolvePathPart(
throw new SemanticException( "Cannot resolve path (`" + name + "`) relative to `" + lhs.getNavigablePath() + "`" );
}
//noinspection unchecked
final SqmPath<?> existingImplicitJoinPath = lhs.getImplicitJoinPath( name );
if ( existingImplicitJoinPath != null ) {
currentPath = existingImplicitJoinPath;
return this;
}

currentPath = subPathSource.createSqmPath( lhs, creationState );
if ( isTerminal ) {
return currentPath;
Expand Down
Expand Up @@ -125,9 +125,19 @@ public void registerImplicitJoinPath(SqmPath<?> path) {
}

final String relativeName = path.getNavigablePath().getLocalName();
if ( !implicitJoinPaths.containsKey( relativeName ) ) {
implicitJoinPaths.put( relativeName, path );

final SqmPath<?> previous = implicitJoinPaths.put( relativeName, path );
if ( previous != null && previous != path ) {
throw new IllegalStateException( "Implicit-join path registration unexpectedly overrode previous registration - " + relativeName );
}
}

@Override
public SqmPath<?> getImplicitJoinPath(String name) {
if ( implicitJoinPaths == null ) {
return null;
}
return implicitJoinPaths.get( name );
}

@Override
Expand Down
Expand Up @@ -77,6 +77,8 @@ public interface SqmPath<T> extends SqmExpression<T>, SemanticPathPart, JpaPath<
*/
void registerImplicitJoinPath(SqmPath<?> path);

SqmPath<?> getImplicitJoinPath(String name);

/**
* This node's type is its "referenced path source"
*/
Expand Down
Expand Up @@ -54,14 +54,35 @@ public void performDataPreparation(SessionFactoryScope scope) {
public void performHqlTest(SessionFactoryScope scope) {
// Now simulate running an audit query
scope.inSession( session -> {
session.createQuery( "select e__ FROM org.hibernate.orm.test.SubQueryImplicitJoinReferenceTest$TheEntity e__ "
session.createQuery( "select e__ FROM TheEntity e__ "
+ "WHERE e__.originalId.rev.id = (select max(e2__.originalId.rev.id) FROM "
+ "org.hibernate.orm.test.SubQueryImplicitJoinReferenceTest$TheEntity e2__ WHERE " +
+ "TheEntity e2__ WHERE " +
"e2__.originalId.rev.id <= 2 and e__.originalId.id = e2__.originalId.id)" ).list();
} );
}

@Entity
@Test
public void performHqlTest2(SessionFactoryScope scope) {
// Now simulate running an audit query
scope.inSession( session -> {
session.createQuery( "select e__ FROM TheEntity e__ "
+ "WHERE e__.originalId.id = (select max(e2__.originalId.id) FROM "
+ "TheEntity e2__ WHERE " +
"e__.originalId.id = e2__.originalId.id and e2__.originalId.rev.id <= 2)" ).list();
} );
}

@Test
public void performHqlTest3(SessionFactoryScope scope) {
// Now simulate running an audit query
scope.inSession( session -> {
session.createQuery( "select e2__.originalId.id, e2__.originalId.rev.id FROM "
+ "TheEntity e2__ WHERE " +
" e2__.originalId.rev.id <= 2" ).list();
} );
}

@Entity(name = "TheEntity")
public static class TheEntity {
@EmbeddedId
private OriginalId originalId;
Expand Down

0 comments on commit 1905e8b

Please sign in to comment.