Fix issue with emneddable id initialization

This commit is contained in:
Andrea Boriero 2020-12-08 14:40:18 +01:00
parent d32fb746d6
commit 7d74f1c727
5 changed files with 70 additions and 56 deletions

View File

@ -78,13 +78,13 @@ public abstract class AbstractImmediateCollectionInitializer extends AbstractCol
return;
}
// if ( CollectionLoadingLogger.TRACE_ENABLED ) {
// CollectionLoadingLogger.INSTANCE.tracef(
// "(%s) Beginning Initializer#resolveInstance for collection : %s",
// getSimpleConcreteImplName(),
// LoggingHelper.toLoggableString( getNavigablePath(), collectionKey.getKey() )
// );
// }
if ( CollectionLoadingLogger.TRACE_ENABLED ) {
CollectionLoadingLogger.INSTANCE.tracef(
"(%s) Beginning Initializer#resolveInstance for collection : %s",
getSimpleConcreteImplName(),
LoggingHelper.toLoggableString( getNavigablePath(), collectionKey.getKey() )
);
}
// determine the PersistentCollection instance to use and whether
// we (this initializer) is responsible for loading its state
@ -195,14 +195,14 @@ public abstract class AbstractImmediateCollectionInitializer extends AbstractCol
session
);
// if ( CollectionLoadingLogger.DEBUG_ENABLED ) {
// CollectionLoadingLogger.INSTANCE.debugf(
// "(%s) Created new collection wrapper [%s] : %s",
// getSimpleConcreteImplName(),
// LoggingHelper.toLoggableString( getNavigablePath(), collectionKey.getKey() ),
// toLoggableString( collectionInstance )
// );
// }
if ( CollectionLoadingLogger.DEBUG_ENABLED ) {
CollectionLoadingLogger.INSTANCE.debugf(
"(%s) Created new collection wrapper [%s] : %s",
getSimpleConcreteImplName(),
LoggingHelper.toLoggableString( getNavigablePath(), collectionKey.getKey() ),
toLoggableString( collectionInstance )
);
}
persistenceContext.addUninitializedCollection( collectionDescriptor, collectionInstance, collectionKey.getKey() );
@ -210,14 +210,14 @@ public abstract class AbstractImmediateCollectionInitializer extends AbstractCol
}
if ( responsibility != null ) {
// if ( CollectionLoadingLogger.DEBUG_ENABLED ) {
// CollectionLoadingLogger.INSTANCE.debugf(
// "(%s) Responsible for loading collection [%s] : %s",
// getSimpleConcreteImplName(),
// LoggingHelper.toLoggableString( getNavigablePath(), collectionKey.getKey() ),
// toLoggableString( collectionInstance )
// );
// }
if ( CollectionLoadingLogger.DEBUG_ENABLED ) {
CollectionLoadingLogger.INSTANCE.debugf(
"(%s) Responsible for loading collection [%s] : %s",
getSimpleConcreteImplName(),
LoggingHelper.toLoggableString( getNavigablePath(), collectionKey.getKey() ),
toLoggableString( collectionInstance )
);
}
if ( getParentAccess() != null ) {
getParentAccess().registerResolutionListener(
@ -287,12 +287,28 @@ public abstract class AbstractImmediateCollectionInitializer extends AbstractCol
collectionAttributeMapping.getCollectionDescriptor(),
keyContainerValue
);
if ( CollectionLoadingLogger.DEBUG_ENABLED ) {
CollectionLoadingLogger.INSTANCE.debugf(
"(%s) Current row collection key : %s",
getSimpleConcreteImplName(),
LoggingHelper.toLoggableString( getNavigablePath(), this.collectionKey.getKey() )
);
}
}
else if ( keyCollectionValue != null ) {
this.collectionKey = new CollectionKey(
collectionAttributeMapping.getCollectionDescriptor(),
keyCollectionValue
);
if ( CollectionLoadingLogger.DEBUG_ENABLED ) {
CollectionLoadingLogger.INSTANCE.debugf(
"(%s) Current row collection key : %s",
getSimpleConcreteImplName(),
LoggingHelper.toLoggableString( getNavigablePath(), this.collectionKey.getKey() )
);
}
}
else {
this.collectionKey = new CollectionKey(
@ -335,14 +351,14 @@ public abstract class AbstractImmediateCollectionInitializer extends AbstractCol
if ( collectionValueKey != null ) {
// the row contains an element in the collection...
// if ( CollectionLoadingLogger.DEBUG_ENABLED ) {
// CollectionLoadingLogger.INSTANCE.debugf(
// "(%s) Reading element from row for collection [%s] -> %s",
// getSimpleConcreteImplName(),
// LoggingHelper.toLoggableString( getNavigablePath(), collectionKey.getKey() ),
// toLoggableString( collectionInstance )
// );
// }
if ( CollectionLoadingLogger.DEBUG_ENABLED ) {
CollectionLoadingLogger.INSTANCE.debugf(
"(%s) Reading element from row for collection [%s] -> %s",
getSimpleConcreteImplName(),
LoggingHelper.toLoggableString( getNavigablePath(), collectionKey.getKey() ),
toLoggableString( collectionInstance )
);
}
responsibility.load(
loadingState -> readCollectionRow( collectionKey, loadingState, rowProcessingState )

View File

@ -40,8 +40,6 @@ public abstract class AbstractEmbeddableInitializer extends AbstractFetchParentA
private final Object[] resolvedValues;
private final boolean createEmptyCompositesEnabled;
private Object compositeInstance;
private boolean keyResolved;
private boolean instanceResolved;
@SuppressWarnings("WeakerAccess")
@ -95,7 +93,7 @@ public abstract class AbstractEmbeddableInitializer extends AbstractFetchParentA
@Override
public void resolveKey(RowProcessingState rowProcessingState) {
if(keyResolved){
if ( compositeInstance != null ) {
return;
}
final PropertyAccess parentInjectionPropertyAccess = embeddedModelPartDescriptor.getParentInjectionAttributePropertyAccess();
@ -118,19 +116,17 @@ public abstract class AbstractEmbeddableInitializer extends AbstractFetchParentA
}
);
}
keyResolved = true;
}
@Override
public void resolveInstance(RowProcessingState rowProcessingState) {
if ( instanceResolved ) {
if ( compositeInstance != null ) {
return;
}
compositeInstance = embeddedModelPartDescriptor.getEmbeddableTypeDescriptor()
.getRepresentationStrategy()
.getInstantiator()
.instantiate( rowProcessingState.getSession().getFactory() );
instanceResolved = true;
EmbeddableLoadingLogger.INSTANCE.debugf(
"Created composite instance [%s] : %s",
navigablePath,
@ -202,8 +198,6 @@ public abstract class AbstractEmbeddableInitializer extends AbstractFetchParentA
@Override
public void finishUpRow(RowProcessingState rowProcessingState) {
compositeInstance = null;
keyResolved = true;
instanceResolved = false;
clearParentResolutionListeners();
}

View File

@ -356,6 +356,7 @@ public abstract class AbstractEntityInitializer extends AbstractFetchParentAcces
identifierInitializers.forEach( initializer -> initializer.resolveKey( rowProcessingState ) );
identifierInitializers.forEach( initializer -> initializer.resolveInstance( rowProcessingState ) );
identifierInitializers.forEach( initializer -> initializer.initializeInstance( rowProcessingState ) );
if ( EntityLoadingLogger.TRACE_ENABLED ) {
EntityLoadingLogger.LOGGER.tracef(
@ -413,7 +414,6 @@ public abstract class AbstractEntityInitializer extends AbstractFetchParentAcces
if ( missing ) {
return;
}
identifierInitializers.forEach( initializer -> initializer.initializeInstance( rowProcessingState ) );
final Object entityIdentifier = entityKey.getIdentifier();

View File

@ -17,7 +17,6 @@ import org.hibernate.testing.jdbc.SQLStatementInspector;
import org.hibernate.testing.orm.junit.DomainModel;
import org.hibernate.testing.orm.junit.SessionFactory;
import org.hibernate.testing.orm.junit.SessionFactoryScope;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
@ -38,17 +37,17 @@ import static org.junit.jupiter.api.Assertions.assertTrue;
@SessionFactory(statementInspectorClass = SQLStatementInspector.class)
public class CompositeIdTest {
@AfterEach
public void tearDown(SessionFactoryScope scope) {
scope.inTransaction(
session -> {
session.createQuery( "delete from LineItem" ).executeUpdate();
session.createQuery( "delete from Order" ).executeUpdate();
session.createQuery( "delete from Customer" ).executeUpdate();
session.createQuery( "delete from Product" ).executeUpdate();
}
);
}
// @AfterEach
// public void tearDown(SessionFactoryScope scope) {
// scope.inTransaction(
// session -> {
// session.createQuery( "delete from LineItem" ).executeUpdate();
// session.createQuery( "delete from Order" ).executeUpdate();
// session.createQuery( "delete from Customer" ).executeUpdate();
// session.createQuery( "delete from Product" ).executeUpdate();
// }
// );
// }
@Test
public void testQuery(SessionFactoryScope scope) {
@ -162,7 +161,7 @@ public class CompositeIdTest {
statementInspector.clear();
session.flush();
statementInspector.assertExecutedCount( 4 );
statementInspector.assertNumberOfOccurrenceInQuery( 0, "select", 1 );
statementInspector.assertIsSelect( 0 );
statementInspector.assertNumberOfOccurrenceInQuery( 0, "join", 0 );
statementInspector.assertIsInsert( 1 );
statementInspector.assertIsUpdate( 2 );
@ -175,11 +174,11 @@ public class CompositeIdTest {
List bigOrders = session.createQuery( "from Order o where o.total>10.0" ).list();
statementInspector.assertExecutedCount( 3 );
statementInspector.assertNumberOfOccurrenceInQuery( 0, "select", 1 );
statementInspector.assertIsSelect( 0 );
statementInspector.assertNumberOfOccurrenceInQuery( 0, "join", 0 );
statementInspector.assertIsInsert( 1 );
statementInspector.assertNumberOfOccurrenceInQuery( 2, "select", 2 );
statementInspector.assertNumberOfOccurrenceInQuery( 3, "join", 0 );
statementInspector.assertIsSelect( 2 );
statementInspector.assertNumberOfOccurrenceInQuery( 2, "join", 0 );
assertEquals( bigOrders.size(), 1 );
}

View File

@ -54,6 +54,11 @@ public class SQLStatementInspector implements StatementInspector {
assertThat( "number of " + toCheck,actual, is( expectedNumberOfOccurrences ) );
}
public void assertIsSelect(int queryNumber) {
String query = sqlQueries.get( queryNumber );
assertTrue( query.toLowerCase( Locale.ROOT ).startsWith( "select" ) );
}
public void assertIsInsert(int queryNumber) {
String query = sqlQueries.get( queryNumber );
assertTrue( query.toLowerCase( Locale.ROOT ).startsWith( "insert" ) );