|
9 | 9 | import java.io.IOException; |
10 | 10 | import java.io.ObjectInputStream; |
11 | 11 | import java.io.ObjectOutputStream; |
12 | | -import java.util.Collections; |
13 | 12 | import java.util.IdentityHashMap; |
14 | 13 | import java.util.Map; |
15 | 14 | import java.util.Set; |
|
25 | 24 | import org.hibernate.event.spi.EventSource; |
26 | 25 | import org.hibernate.internal.CoreMessageLogger; |
27 | 26 | import org.hibernate.internal.util.collections.IdentitySet; |
28 | | -import org.hibernate.pretty.MessageHelper; |
29 | 27 |
|
30 | 28 | import org.jboss.logging.Logger; |
31 | 29 |
|
| 30 | +import static java.util.Collections.emptySet; |
| 31 | +import static org.hibernate.pretty.MessageHelper.infoString; |
| 32 | + |
32 | 33 | /** |
33 | 34 | * Tracks unresolved entity insert actions. |
34 | | - * |
| 35 | + * <p> |
35 | 36 | * An entity insert action is unresolved if the entity |
36 | 37 | * to be inserted has at least one non-nullable association with |
37 | 38 | * an unsaved transient entity, and the foreign key points to that |
38 | 39 | * unsaved transient entity. |
39 | | - * |
| 40 | + * <p> |
40 | 41 | * These references must be resolved before an insert action can be |
41 | 42 | * executed. |
42 | 43 | * |
@@ -134,11 +135,11 @@ private void logCannotResolveNonNullableTransientDependencies(SharedSessionContr |
134 | 135 | .getMappingMetamodel() |
135 | 136 | .getEntityDescriptor( transientEntityName ) |
136 | 137 | .getIdentifier( transientEntity, session ); |
137 | | - final String transientEntityString = MessageHelper.infoString( transientEntityName, transientEntityId ); |
| 138 | + final String transientEntityString = infoString( transientEntityName, transientEntityId ); |
138 | 139 | final Set<String> dependentEntityStrings = new TreeSet<>(); |
139 | 140 | final Set<String> nonNullableTransientPropertyPaths = new TreeSet<>(); |
140 | 141 | for ( AbstractEntityInsertAction dependentAction : entry.getValue() ) { |
141 | | - dependentEntityStrings.add( MessageHelper.infoString( dependentAction.getEntityName(), dependentAction.getId() ) ); |
| 142 | + dependentEntityStrings.add( infoString( dependentAction.getEntityName(), dependentAction.getId() ) ); |
142 | 143 | for ( String path : dependenciesByAction.get( dependentAction ).getNonNullableTransientPropertyPaths( transientEntity ) ) { |
143 | 144 | final String fullPath = dependentAction.getEntityName() + '.' + path; |
144 | 145 | nonNullableTransientPropertyPaths.add( fullPath ); |
@@ -196,51 +197,52 @@ public Set<AbstractEntityInsertAction> resolveDependentActions(Object managedEnt |
196 | 197 | if ( traceEnabled ) { |
197 | 198 | LOG.tracev( |
198 | 199 | "No unresolved entity inserts that depended on [{0}]", |
199 | | - MessageHelper.infoString( entityEntry.getEntityName(), entityEntry.getId() ) |
| 200 | + infoString( entityEntry.getEntityName(), entityEntry.getId() ) |
200 | 201 | ); |
201 | 202 | } |
202 | | - // NOTE EARLY EXIT! |
203 | | - return Collections.emptySet(); |
204 | | - } |
205 | | - final Set<AbstractEntityInsertAction> resolvedActions = new IdentitySet<>( ); |
206 | | - if ( traceEnabled ) { |
207 | | - LOG.tracev( |
208 | | - "Unresolved inserts before resolving [{0}]: [{1}]", |
209 | | - MessageHelper.infoString( entityEntry.getEntityName(), entityEntry.getId() ), |
210 | | - toString() |
211 | | - ); |
| 203 | + return emptySet(); |
212 | 204 | } |
213 | | - for ( AbstractEntityInsertAction dependentAction : dependentActions ) { |
214 | | - if ( traceEnabled ) { |
| 205 | + else { |
| 206 | + final Set<AbstractEntityInsertAction> resolvedActions = new IdentitySet<>( ); |
| 207 | + if ( traceEnabled ) { |
215 | 208 | LOG.tracev( |
216 | | - "Resolving insert [{0}] dependency on [{1}]", |
217 | | - MessageHelper.infoString( dependentAction.getEntityName(), dependentAction.getId() ), |
218 | | - MessageHelper.infoString( entityEntry.getEntityName(), entityEntry.getId() ) |
| 209 | + "Unresolved inserts before resolving [{0}]: [{1}]", |
| 210 | + infoString( entityEntry.getEntityName(), entityEntry.getId() ), |
| 211 | + toString() |
219 | 212 | ); |
220 | 213 | } |
221 | | - final NonNullableTransientDependencies dependencies = dependenciesByAction.get( dependentAction ); |
222 | | - dependencies.resolveNonNullableTransientEntity( managedEntity ); |
223 | | - if ( dependencies.isEmpty() ) { |
| 214 | + for ( AbstractEntityInsertAction dependentAction : dependentActions ) { |
224 | 215 | if ( traceEnabled ) { |
225 | 216 | LOG.tracev( |
226 | | - "Resolving insert [{0}] (only depended on [{1}])", |
227 | | - dependentAction, |
228 | | - MessageHelper.infoString( entityEntry.getEntityName(), entityEntry.getId() ) |
| 217 | + "Resolving insert [{0}] dependency on [{1}]", |
| 218 | + infoString( dependentAction.getEntityName(), dependentAction.getId() ), |
| 219 | + infoString( entityEntry.getEntityName(), entityEntry.getId() ) |
229 | 220 | ); |
230 | 221 | } |
231 | | - // dependentAction only depended on managedEntity.. |
232 | | - dependenciesByAction.remove( dependentAction ); |
233 | | - resolvedActions.add( dependentAction ); |
| 222 | + final NonNullableTransientDependencies dependencies = dependenciesByAction.get( dependentAction ); |
| 223 | + dependencies.resolveNonNullableTransientEntity( managedEntity ); |
| 224 | + if ( dependencies.isEmpty() ) { |
| 225 | + if ( traceEnabled ) { |
| 226 | + LOG.tracev( |
| 227 | + "Resolving insert [{0}] (only depended on [{1}])", |
| 228 | + dependentAction, |
| 229 | + infoString( entityEntry.getEntityName(), entityEntry.getId() ) |
| 230 | + ); |
| 231 | + } |
| 232 | + // dependentAction only depended on managedEntity.. |
| 233 | + dependenciesByAction.remove( dependentAction ); |
| 234 | + resolvedActions.add( dependentAction ); |
| 235 | + } |
234 | 236 | } |
| 237 | + if ( traceEnabled ) { |
| 238 | + LOG.tracev( |
| 239 | + "Unresolved inserts after resolving [{0}]: [{1}]", |
| 240 | + infoString( entityEntry.getEntityName(), entityEntry.getId() ), |
| 241 | + toString() |
| 242 | + ); |
| 243 | + } |
| 244 | + return resolvedActions; |
235 | 245 | } |
236 | | - if ( traceEnabled ) { |
237 | | - LOG.tracev( |
238 | | - "Unresolved inserts after resolving [{0}]: [{1}]", |
239 | | - MessageHelper.infoString( entityEntry.getEntityName(), entityEntry.getId() ), |
240 | | - toString() |
241 | | - ); |
242 | | - } |
243 | | - return resolvedActions; |
244 | 246 | } |
245 | 247 |
|
246 | 248 | /** |
|
0 commit comments