Skip to content

Commit 74e1e6c

Browse files
committed
catch other cases where NPE indicates that a PostInitCallbackEntry is unready
1 parent 9a98976 commit 74e1e6c

File tree

4 files changed

+54
-12
lines changed

4 files changed

+54
-12
lines changed

hibernate-core/src/main/java/org/hibernate/dialect/temptable/TemporaryTable.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import org.hibernate.metamodel.mapping.EntityDiscriminatorMapping;
3232
import org.hibernate.metamodel.mapping.EntityIdentifierMapping;
3333
import org.hibernate.metamodel.mapping.EntityMappingType;
34+
import org.hibernate.metamodel.mapping.ForeignKeyDescriptor;
3435
import org.hibernate.metamodel.mapping.JdbcMapping;
3536
import org.hibernate.metamodel.mapping.ModelPart;
3637
import org.hibernate.metamodel.mapping.PluralAttributeMapping;
@@ -149,7 +150,16 @@ public static TemporaryTable createIdTable(
149150

150151
if ( pluralAttribute.getSeparateCollectionTable() != null ) {
151152
// Ensure that the FK target columns are available
152-
final ModelPart fkTarget = pluralAttribute.getKeyDescriptor().getTargetPart();
153+
ForeignKeyDescriptor keyDescriptor = pluralAttribute.getKeyDescriptor();
154+
if ( keyDescriptor==null ) {
155+
// This is expected to happen when processing a
156+
// PostInitCallbackEntry because the callbacks
157+
// are not ordered. The exception is caught in
158+
// MappingModelCreationProcess.executePostInitCallbacks()
159+
// and the callback is re-queued.
160+
throw new IllegalStateException( "Not yet ready: " + pluralAttribute );
161+
}
162+
final ModelPart fkTarget = keyDescriptor.getTargetPart();
153163
if ( !( fkTarget instanceof EntityIdentifierMapping ) ) {
154164
final Value value = entityBinding.getSubclassProperty( pluralAttribute.getAttributeName() )
155165
.getValue();

hibernate-core/src/main/java/org/hibernate/metamodel/mapping/internal/EmbeddableMappingTypeImpl.java

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
import org.hibernate.metamodel.mapping.EmbeddableMappingType;
3939
import org.hibernate.metamodel.mapping.EmbeddableValuedModelPart;
4040
import org.hibernate.metamodel.mapping.EntityMappingType;
41+
import org.hibernate.metamodel.mapping.ForeignKeyDescriptor;
4142
import org.hibernate.metamodel.mapping.JdbcMapping;
4243
import org.hibernate.metamodel.mapping.ManagedMappingType;
4344
import org.hibernate.metamodel.mapping.ModelPart;
@@ -195,6 +196,15 @@ public EmbeddableMappingTypeImpl(
195196
}
196197
else if ( attributeMapping instanceof ToOneAttributeMapping ) {
197198
final ToOneAttributeMapping original = (ToOneAttributeMapping) attributeMapping;
199+
ForeignKeyDescriptor foreignKeyDescriptor = original.getForeignKeyDescriptor();
200+
if ( foreignKeyDescriptor==null ) {
201+
// This is expected to happen when processing a
202+
// PostInitCallbackEntry because the callbacks
203+
// are not ordered. The exception is caught in
204+
// MappingModelCreationProcess.executePostInitCallbacks()
205+
// and the callback is re-queued.
206+
throw new IllegalStateException( "Not yet ready: " + original );
207+
}
198208
final ToOneAttributeMapping toOne = original.copy(
199209
declaringType,
200210
declaringTableGroupProducer
@@ -204,7 +214,7 @@ else if ( attributeMapping instanceof ToOneAttributeMapping ) {
204214
selectableMappings.getSelectable( offset ).getContainingTableExpression()
205215
);
206216
toOne.setForeignKeyDescriptor(
207-
original.getForeignKeyDescriptor().withKeySelectionMapping(
217+
foreignKeyDescriptor.withKeySelectionMapping(
208218
declaringType,
209219
declaringTableGroupProducer,
210220
index -> selectableMappings.getSelectable( offset + index ),
@@ -704,7 +714,7 @@ private SelectableMappings getSelectableMappings() {
704714
// are not ordered. The exception is caught in
705715
// MappingModelCreationProcess.executePostInitCallbacks()
706716
// and the callback is re-queued.
707-
throw new IllegalStateException("not yet ready");
717+
throw new IllegalStateException("Not yet ready");
708718
}
709719
return selectableMappings;
710720
}

hibernate-core/src/main/java/org/hibernate/metamodel/mapping/internal/EntityCollectionPart.java

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,12 @@ public void finishInitialization(
213213
.getAssociatedEntityPersister( creationProcess.getCreationContext().getSessionFactory() );
214214
fkTargetModelPart = elementPersister.findByPath( mapKeyPropertyName );
215215
if ( fkTargetModelPart == null ) {
216-
throw new RuntimeException( "Couldn't find model part for path [" + mapKeyPropertyName + "] on entity: " + elementPersister.getEntityName() );
216+
// This is expected to happen when processing a
217+
// PostInitCallbackEntry because the callbacks
218+
// are not ordered. The exception is caught in
219+
// MappingModelCreationProcess.executePostInitCallbacks()
220+
// and the callback is re-queued.
221+
throw new IllegalStateException( "Couldn't find model part for path [" + mapKeyPropertyName + "] on entity: " + elementPersister.getEntityName() );
217222
}
218223
}
219224
}
@@ -222,7 +227,12 @@ public void finishInitialization(
222227
if ( collectionDescriptor.isOneToMany() && mappedByProperty != null && !mappedByProperty.isEmpty() ) {
223228
fkTargetModelPart = entityMappingType.findByPath( mappedByProperty );
224229
if ( fkTargetModelPart == null ) {
225-
throw new RuntimeException( "Couldn't find model part for path [" + mappedByProperty + "] on entity: " + entityMappingType.getEntityName() );
230+
// This is expected to happen when processing a
231+
// PostInitCallbackEntry because the callbacks
232+
// are not ordered. The exception is caught in
233+
// MappingModelCreationProcess.executePostInitCallbacks()
234+
// and the callback is re-queued.
235+
throw new IllegalStateException( "Couldn't find model part for path [" + mappedByProperty + "] on entity: " + entityMappingType.getEntityName() );
226236
}
227237
}
228238
else {
@@ -261,7 +271,7 @@ private ForeignKeyDescriptor createForeignKeyDescriptor(
261271
if ( fkTargetModelPart instanceof ToOneAttributeMapping ) {
262272
final ToOneAttributeMapping toOneAttributeMapping = (ToOneAttributeMapping) fkTargetModelPart;
263273
if ( toOneAttributeMapping.getForeignKeyDescriptor() == null ) {
264-
throw new RuntimeException( "Not yet ready: " + toOneAttributeMapping );
274+
throw new IllegalStateException( "Not yet ready: " + toOneAttributeMapping );
265275
}
266276
return toOneAttributeMapping.getForeignKeyDescriptor();
267277
}

hibernate-core/src/main/java/org/hibernate/metamodel/mapping/internal/VirtualIdEmbeddable.java

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -149,32 +149,44 @@ public AttributeMapping findAttributeMapping(String name) {
149149

150150
@Override
151151
public SelectableMapping getSelectable(int columnIndex) {
152-
return selectableMappings.getSelectable( columnIndex );
152+
return getSelectableMappings().getSelectable( columnIndex );
153153
}
154154

155155
@Override
156156
public int forEachSelectable(SelectableConsumer consumer) {
157-
return selectableMappings.forEachSelectable( 0, consumer );
157+
return getSelectableMappings().forEachSelectable( 0, consumer );
158158
}
159159

160160
@Override
161161
public int forEachSelectable(int offset, SelectableConsumer consumer) {
162-
return selectableMappings.forEachSelectable( offset, consumer );
162+
return getSelectableMappings().forEachSelectable( offset, consumer );
163163
}
164164

165165
@Override
166166
public int getJdbcTypeCount() {
167-
return selectableMappings.getJdbcTypeCount();
167+
return getSelectableMappings().getJdbcTypeCount();
168168
}
169169

170170
@Override
171171
public List<JdbcMapping> getJdbcMappings() {
172-
return selectableMappings.getJdbcMappings();
172+
return getSelectableMappings().getJdbcMappings();
173+
}
174+
175+
private SelectableMappings getSelectableMappings() {
176+
if (selectableMappings == null) {
177+
// This is expected to happen when processing a
178+
// PostInitCallbackEntry because the callbacks
179+
// are not ordered. The exception is caught in
180+
// MappingModelCreationProcess.executePostInitCallbacks()
181+
// and the callback is re-queued.
182+
throw new IllegalStateException("Not yet ready");
183+
}
184+
return selectableMappings;
173185
}
174186

175187
@Override
176188
public int forEachJdbcType(int offset, IndexedConsumer<JdbcMapping> action) {
177-
return selectableMappings.forEachSelectable(
189+
return getSelectableMappings().forEachSelectable(
178190
offset,
179191
(index, selectable) -> action.accept( index, selectable.getJdbcMapping() )
180192
);

0 commit comments

Comments
 (0)