99import org .hibernate .collection .spi .PersistentCollection ;
1010import org .hibernate .internal .CoreLogging ;
1111import org .hibernate .internal .CoreMessageLogger ;
12- import org .hibernate .internal .util .NullnessUtil ;
1312import org .hibernate .persister .collection .CollectionPersister ;
1413
1514import java .io .IOException ;
2019
2120import org .checkerframework .checker .nullness .qual .Nullable ;
2221
22+ import static org .hibernate .internal .util .NullnessUtil .castNonNull ;
2323import static org .hibernate .pretty .MessageHelper .collectionInfoString ;
2424
2525/**
@@ -68,11 +68,11 @@ public CollectionEntry(CollectionPersister persister, PersistentCollection<?> co
6868 // during flush shouldn't be ignored
6969 ignore = false ;
7070
71- collection .clearDirty (); //a newly wrapped collection is NOT dirty (or we get unnecessary version updates)
71+ // a newly wrapped collection is NOT dirty
72+ // (or we get unnecessary version updates)
73+ collection .clearDirty ();
7274
73- snapshot = persister .isMutable () ?
74- collection .getSnapshot ( persister ) :
75- null ;
75+ snapshot = persister .isMutable () ? collection .getSnapshot ( persister ) : null ;
7676 collection .setSnapshot ( loadedKey , role , snapshot );
7777 }
7878
@@ -123,7 +123,7 @@ public CollectionEntry(PersistentCollection<?> collection, SessionFactoryImpleme
123123
124124 loadedKey = collection .getKey ();
125125 role = collection .getRole ();
126- loadedPersister = factory .getRuntimeMetamodels (). getMappingMetamodel ().getCollectionDescriptor ( NullnessUtil . castNonNull ( role ) );
126+ loadedPersister = factory .getMappingMetamodel ().getCollectionDescriptor ( castNonNull ( role ) );
127127
128128 snapshot = collection .getStoredSnapshot ();
129129 }
@@ -156,12 +156,11 @@ private void dirty(PersistentCollection<?> collection) throws HibernateException
156156 final CollectionPersister loadedPersister = getLoadedPersister ();
157157 final boolean forceDirty =
158158 collection .wasInitialized ()
159- && !collection .isDirty () //optimization
160- && loadedPersister != null
161- && loadedPersister .isMutable () //optimization
162- && ( collection .isDirectlyAccessible () || loadedPersister .getElementType ().isMutable () ) //optimization
163- && !collection .equalsSnapshot ( loadedPersister );
164-
159+ && !collection .isDirty () //optimization
160+ && loadedPersister != null
161+ && loadedPersister .isMutable () //optimization
162+ && ( collection .isDirectlyAccessible () || loadedPersister .getElementType ().isMutable () ) //optimization
163+ && !collection .equalsSnapshot ( loadedPersister );
165164 if ( forceDirty ) {
166165 collection .dirty ();
167166 }
@@ -174,23 +173,19 @@ public void preFlush(PersistentCollection<?> collection) throws HibernateExcepti
174173 }
175174
176175 final CollectionPersister loadedPersister = getLoadedPersister ();
177- boolean nonMutableChange = collection .isDirty ()
178- && loadedPersister != null
179- && !loadedPersister .isMutable ();
176+ final boolean nonMutableChange =
177+ collection .isDirty ()
178+ && loadedPersister != null
179+ && !loadedPersister .isMutable ();
180180 if ( nonMutableChange ) {
181- throw new HibernateException (
182- "changed an immutable collection instance: " +
183- collectionInfoString ( NullnessUtil .castNonNull ( loadedPersister ).getRole (), getLoadedKey () )
184- );
181+ throw new HibernateException ( "changed an immutable collection instance: " +
182+ collectionInfoString ( castNonNull ( loadedPersister ).getRole (), getLoadedKey () ) );
185183 }
186184
187185 dirty ( collection );
188186
189187 if ( LOG .isDebugEnabled () && collection .isDirty () && loadedPersister != null ) {
190- LOG .debugf (
191- "Collection dirty: %s" ,
192- collectionInfoString ( loadedPersister .getRole (), getLoadedKey () )
193- );
188+ LOG .debug ( "Collection dirty: " + collectionInfoString ( loadedPersister .getRole (), getLoadedKey () ) );
194189 }
195190
196191 setReached ( false );
@@ -208,9 +203,9 @@ public void postInitialize(PersistentCollection<?> collection, SharedSessionCont
208203 ? collection .getSnapshot ( loadedPersister )
209204 : null ;
210205 collection .setSnapshot ( loadedKey , role , snapshot );
211- if ( loadedPersister != null && session . getLoadQueryInfluencers (). effectivelyBatchLoadable ( loadedPersister ) ) {
212- session .getPersistenceContextInternal ()
213- .getBatchFetchQueue ()
206+ if ( loadedPersister != null
207+ && session .getLoadQueryInfluencers (). effectivelyBatchLoadable ( loadedPersister ) ) {
208+ session . getPersistenceContextInternal () .getBatchFetchQueue ()
214209 .removeBatchLoadableCollection ( this );
215210 }
216211 }
@@ -236,12 +231,12 @@ public void afterAction(PersistentCollection<?> collection) {
236231 loadedKey = getCurrentKey ();
237232 setLoadedPersister ( getCurrentPersister () );
238233
239- boolean resnapshot = collection .wasInitialized ()
234+ final boolean resnapshot = collection .wasInitialized ()
240235 && ( isDoremove () || isDorecreate () || isDoupdate () );
241236 if ( resnapshot ) {
242- snapshot = loadedPersister == null || ! loadedPersister .isMutable () ?
243- null :
244- collection . getSnapshot ( NullnessUtil . castNonNull ( loadedPersister ) ) ; //re-snapshot
237+ snapshot = loadedPersister != null && loadedPersister .isMutable ()
238+ ? collection . getSnapshot ( castNonNull ( loadedPersister ) )
239+ : null ; //re-snapshot
245240 }
246241
247242 collection .postAction ();
@@ -271,13 +266,11 @@ public void afterAction(PersistentCollection<?> collection) {
271266 public void resetStoredSnapshot (PersistentCollection <?> collection , Serializable storedSnapshot ) {
272267 LOG .debugf ("Reset storedSnapshot to %s for %s" , storedSnapshot , this );
273268
274- if ( fromMerge ) {
275- return ; // EARLY EXIT!
269+ if ( !fromMerge ) {
270+ snapshot = storedSnapshot ;
271+ collection .setSnapshot ( loadedKey , role , snapshot );
272+ fromMerge = true ;
276273 }
277-
278- snapshot = storedSnapshot ;
279- collection .setSnapshot ( loadedKey , role , snapshot );
280- fromMerge = true ;
281274 }
282275
283276 private void setLoadedPersister (@ Nullable CollectionPersister persister ) {
@@ -286,12 +279,9 @@ private void setLoadedPersister(@Nullable CollectionPersister persister) {
286279 }
287280
288281 void afterDeserialize (@ Nullable SessionFactoryImplementor factory ) {
289- if ( factory == null ) {
290- loadedPersister = null ;
291- }
292- else {
293- loadedPersister = factory .getRuntimeMetamodels ().getMappingMetamodel ().getCollectionDescriptor ( NullnessUtil .castNonNull ( role ) );
294- }
282+ loadedPersister = factory == null ? null
283+ : factory .getRuntimeMetamodels ().getMappingMetamodel ()
284+ .getCollectionDescriptor ( castNonNull ( role ) );
295285 }
296286
297287 public boolean wasDereferenced () {
@@ -379,13 +369,15 @@ public void setRole(@Nullable String role) {
379369
380370 @ Override
381371 public String toString () {
382- String result = "CollectionEntry" +
383- collectionInfoString ( role , loadedKey );
384- if ( currentPersister != null ) {
385- result += "->" +
386- collectionInfoString ( currentPersister .getRole (), currentKey );
372+ final StringBuilder result =
373+ new StringBuilder ( "CollectionEntry" )
374+ .append ( collectionInfoString ( role , loadedKey ) );
375+ final CollectionPersister persister = currentPersister ;
376+ if ( persister != null ) {
377+ result .append ( "->" )
378+ .append ( collectionInfoString ( persister .getRole (), currentKey ) );
387379 }
388- return result ;
380+ return result . toString () ;
389381 }
390382
391383 /**
@@ -432,9 +424,8 @@ public void serialize(ObjectOutputStream oos) throws IOException {
432424 *
433425 * @return The deserialized CollectionEntry
434426 */
435- public static CollectionEntry deserialize (
436- ObjectInputStream ois ,
437- SessionImplementor session ) throws IOException , ClassNotFoundException {
427+ public static CollectionEntry deserialize (ObjectInputStream ois , SessionImplementor session )
428+ throws IOException , ClassNotFoundException {
438429 return new CollectionEntry (
439430 (String ) ois .readObject (),
440431 (Serializable ) ois .readObject (),
0 commit comments