HHH-16744 Wrong class created when reusing a join column for different entities in multiple ManyToOne

This commit is contained in:
Andrea Boriero 2023-06-12 19:12:32 +02:00 committed by Andrea Boriero
parent 7223a5eb53
commit 4878a1d277
4 changed files with 48 additions and 38 deletions

View File

@ -35,6 +35,8 @@ public abstract class AbstractBatchEntitySelectFetchInitializer extends Abstract
protected Object entityInstance;
protected EntityKey entityKey;
protected State state = State.UNINITIALIZED;
public AbstractBatchEntitySelectFetchInitializer(
FetchParentAccess parentAccess,
ToOneAttributeMapping referencedModelPart,
@ -61,20 +63,6 @@ public abstract class AbstractBatchEntitySelectFetchInitializer extends Abstract
@Override
public void resolveKey(RowProcessingState rowProcessingState) {
final Object entityIdentifier = identifierAssembler.assemble( rowProcessingState );
if ( entityIdentifier == null ) {
return;
}
entityKey = new EntityKey( entityIdentifier, concreteDescriptor );
rowProcessingState.getSession().getPersistenceContext()
.getBatchFetchQueue().addBatchLoadableEntityKey( entityKey );
registerResolutionListener();
}
@Override
public void resolveInstance(RowProcessingState rowProcessingState) {
}
@Override
@ -83,10 +71,38 @@ public abstract class AbstractBatchEntitySelectFetchInitializer extends Abstract
protected abstract void registerResolutionListener();
protected void resolveKey(
RowProcessingState rowProcessingState,
ToOneAttributeMapping referencedModelPart,
FetchParentAccess parentAccess) {
if ( state != State.UNINITIALIZED ) {
return;
}
if ( !isAttributeAssignableToConcreteDescriptor( parentAccess, referencedModelPart ) ) {
state = State.MISSING;
return;
}
final Object entityIdentifier = identifierAssembler.assemble( rowProcessingState );
if ( entityIdentifier == null ) {
state = State.MISSING;
}
else {
entityKey = new EntityKey( entityIdentifier, concreteDescriptor );
state = State.KEY_RESOLVED;
rowProcessingState.getSession().getPersistenceContext()
.getBatchFetchQueue().addBatchLoadableEntityKey( entityKey );
registerResolutionListener();
}
}
@Override
public void finishUpRow(RowProcessingState rowProcessingState) {
entityInstance = null;
entityKey = null;
state = State.UNINITIALIZED;
clearResolutionListeners();
}
@ -154,4 +170,11 @@ public abstract class AbstractBatchEntitySelectFetchInitializer extends Abstract
return concreteDescriptor;
}
enum State {
UNINITIALIZED,
MISSING,
KEY_RESOLVED,
INITIALIZED
}
}

View File

@ -63,6 +63,7 @@ public class BatchEntityInsideEmbeddableSelectFetchInitializer extends AbstractB
@Override
public void resolveInstance(RowProcessingState rowProcessingState) {
resolveKey( rowProcessingState, referencedModelPart, parentAccess );
if ( entityKey == null ) {
return;
}

View File

@ -23,6 +23,7 @@ import org.hibernate.sql.exec.spi.ExecutionContext;
import org.hibernate.sql.results.graph.DomainResultAssembler;
import org.hibernate.sql.results.graph.FetchParentAccess;
import org.hibernate.sql.results.graph.entity.EntityInitializer;
import org.hibernate.sql.results.jdbc.spi.RowProcessingState;
public class BatchEntitySelectFetchInitializer extends AbstractBatchEntitySelectFetchInitializer {
private Map<EntityKey, List<ParentInfo>> toBatchLoad;
@ -36,6 +37,11 @@ public class BatchEntitySelectFetchInitializer extends AbstractBatchEntitySelect
super( parentAccess, referencedModelPart, fetchedNavigable, concreteDescriptor, identifierAssembler );
}
@Override
public void resolveInstance(RowProcessingState rowProcessingState) {
resolveKey( rowProcessingState, referencedModelPart, parentAccess );
}
@Override
protected void registerResolutionListener() {
parentAccess.registerResolutionListener( parentInstance -> {

View File

@ -28,7 +28,7 @@ import org.hibernate.sql.results.jdbc.spi.RowProcessingState;
public class BatchInitializeEntitySelectFetchInitializer extends AbstractBatchEntitySelectFetchInitializer {
private final Set<EntityKey> toBatchLoad = new HashSet<>();
private State state = State.UNINITIALIZED;
public BatchInitializeEntitySelectFetchInitializer(
FetchParentAccess parentAccess,
@ -44,18 +44,11 @@ public class BatchInitializeEntitySelectFetchInitializer extends AbstractBatchEn
// No-op, because we resolve a proxy
}
@Override
public void resolveKey(RowProcessingState rowProcessingState) {
if ( state != State.UNINITIALIZED ) {
return;
}
super.resolveKey( rowProcessingState );
state = entityKey == null ? State.MISSING : State.KEY_RESOLVED;
}
@Override
public void resolveInstance(RowProcessingState rowProcessingState) {
if ( state != State.KEY_RESOLVED ) {
resolveKey( rowProcessingState, referencedModelPart, parentAccess );
if ( entityKey == null ) {
return;
}
@ -87,12 +80,6 @@ public class BatchInitializeEntitySelectFetchInitializer extends AbstractBatchEn
return state == State.INITIALIZED;
}
@Override
public void finishUpRow(RowProcessingState rowProcessingState) {
super.finishUpRow( rowProcessingState );
state = State.UNINITIALIZED;
}
@Override
public void endLoading(ExecutionContext context) {
final SharedSessionContractImplementor session = context.getSession();
@ -107,11 +94,4 @@ public class BatchInitializeEntitySelectFetchInitializer extends AbstractBatchEn
return "BatchInitializeEntitySelectFetchInitializer(" + LoggingHelper.toLoggableString( getNavigablePath() ) + ")";
}
enum State {
UNINITIALIZED,
MISSING,
KEY_RESOLVED,
INITIALIZED
}
}