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

View File

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

View File

@ -356,6 +356,7 @@ public abstract class AbstractEntityInitializer extends AbstractFetchParentAcces
identifierInitializers.forEach( initializer -> initializer.resolveKey( rowProcessingState ) ); identifierInitializers.forEach( initializer -> initializer.resolveKey( rowProcessingState ) );
identifierInitializers.forEach( initializer -> initializer.resolveInstance( rowProcessingState ) ); identifierInitializers.forEach( initializer -> initializer.resolveInstance( rowProcessingState ) );
identifierInitializers.forEach( initializer -> initializer.initializeInstance( rowProcessingState ) );
if ( EntityLoadingLogger.TRACE_ENABLED ) { if ( EntityLoadingLogger.TRACE_ENABLED ) {
EntityLoadingLogger.LOGGER.tracef( EntityLoadingLogger.LOGGER.tracef(
@ -413,7 +414,6 @@ public abstract class AbstractEntityInitializer extends AbstractFetchParentAcces
if ( missing ) { if ( missing ) {
return; return;
} }
identifierInitializers.forEach( initializer -> initializer.initializeInstance( rowProcessingState ) );
final Object entityIdentifier = entityKey.getIdentifier(); 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.DomainModel;
import org.hibernate.testing.orm.junit.SessionFactory; import org.hibernate.testing.orm.junit.SessionFactory;
import org.hibernate.testing.orm.junit.SessionFactoryScope; import org.hibernate.testing.orm.junit.SessionFactoryScope;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertEquals;
@ -38,17 +37,17 @@ import static org.junit.jupiter.api.Assertions.assertTrue;
@SessionFactory(statementInspectorClass = SQLStatementInspector.class) @SessionFactory(statementInspectorClass = SQLStatementInspector.class)
public class CompositeIdTest { public class CompositeIdTest {
@AfterEach // @AfterEach
public void tearDown(SessionFactoryScope scope) { // public void tearDown(SessionFactoryScope scope) {
scope.inTransaction( // scope.inTransaction(
session -> { // session -> {
session.createQuery( "delete from LineItem" ).executeUpdate(); // session.createQuery( "delete from LineItem" ).executeUpdate();
session.createQuery( "delete from Order" ).executeUpdate(); // session.createQuery( "delete from Order" ).executeUpdate();
session.createQuery( "delete from Customer" ).executeUpdate(); // session.createQuery( "delete from Customer" ).executeUpdate();
session.createQuery( "delete from Product" ).executeUpdate(); // session.createQuery( "delete from Product" ).executeUpdate();
} // }
); // );
} // }
@Test @Test
public void testQuery(SessionFactoryScope scope) { public void testQuery(SessionFactoryScope scope) {
@ -162,7 +161,7 @@ public class CompositeIdTest {
statementInspector.clear(); statementInspector.clear();
session.flush(); session.flush();
statementInspector.assertExecutedCount( 4 ); statementInspector.assertExecutedCount( 4 );
statementInspector.assertNumberOfOccurrenceInQuery( 0, "select", 1 ); statementInspector.assertIsSelect( 0 );
statementInspector.assertNumberOfOccurrenceInQuery( 0, "join", 0 ); statementInspector.assertNumberOfOccurrenceInQuery( 0, "join", 0 );
statementInspector.assertIsInsert( 1 ); statementInspector.assertIsInsert( 1 );
statementInspector.assertIsUpdate( 2 ); statementInspector.assertIsUpdate( 2 );
@ -175,11 +174,11 @@ public class CompositeIdTest {
List bigOrders = session.createQuery( "from Order o where o.total>10.0" ).list(); List bigOrders = session.createQuery( "from Order o where o.total>10.0" ).list();
statementInspector.assertExecutedCount( 3 ); statementInspector.assertExecutedCount( 3 );
statementInspector.assertNumberOfOccurrenceInQuery( 0, "select", 1 ); statementInspector.assertIsSelect( 0 );
statementInspector.assertNumberOfOccurrenceInQuery( 0, "join", 0 ); statementInspector.assertNumberOfOccurrenceInQuery( 0, "join", 0 );
statementInspector.assertIsInsert( 1 ); statementInspector.assertIsInsert( 1 );
statementInspector.assertNumberOfOccurrenceInQuery( 2, "select", 2 ); statementInspector.assertIsSelect( 2 );
statementInspector.assertNumberOfOccurrenceInQuery( 3, "join", 0 ); statementInspector.assertNumberOfOccurrenceInQuery( 2, "join", 0 );
assertEquals( bigOrders.size(), 1 ); assertEquals( bigOrders.size(), 1 );
} }

View File

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