33
33
import org .hibernate .MappingException ;
34
34
import org .hibernate .NonUniqueObjectException ;
35
35
import org .hibernate .PersistentObjectException ;
36
- import org .hibernate .TransientObjectException ;
37
36
import org .hibernate .bytecode .enhance .spi .interceptor .BytecodeLazyAttributeInterceptor ;
38
37
import org .hibernate .bytecode .enhance .spi .interceptor .EnhancementAsProxyLazinessInterceptor ;
39
38
import org .hibernate .collection .spi .PersistentCollection ;
79
78
import static org .hibernate .engine .internal .ManagedTypeHelper .asManagedEntity ;
80
79
import static org .hibernate .engine .internal .ManagedTypeHelper .asPersistentAttributeInterceptable ;
81
80
import static org .hibernate .engine .internal .ManagedTypeHelper .isPersistentAttributeInterceptable ;
81
+ import static org .hibernate .proxy .HibernateProxy .extractLazyInitializer ;
82
82
83
83
/**
84
84
* A <em>stateful</em> implementation of the {@link PersistenceContext} contract, meaning that we maintain this
@@ -217,7 +217,7 @@ public boolean hasLoadContext() {
217
217
//
218
218
@ Override
219
219
public PersistentCollection <?> useUnownedCollection (CollectionKey key ) {
220
- return ( unownedCollections == null ) ? null : unownedCollections .remove ( key );
220
+ return unownedCollections == null ? null : unownedCollections .remove ( key );
221
221
}
222
222
223
223
@ Override
@@ -234,7 +234,7 @@ public void clear() {
234
234
//Strictly avoid lambdas in this case
235
235
for ( EntityHolderImpl value : entitiesByKey .values () ) {
236
236
if ( value != null && value .proxy != null ) {
237
- HibernateProxy . extractLazyInitializer ( value .proxy ).unsetSession ();
237
+ extractLazyInitializer ( value .proxy ).unsetSession ();
238
238
}
239
239
}
240
240
}
@@ -337,9 +337,8 @@ public Object getNaturalIdSnapshot(Object id, EntityPersister persister) throws
337
337
if ( cachedValue != null ) {
338
338
return cachedValue ;
339
339
}
340
-
341
340
// check to see if the natural id is mutable/immutable
342
- if ( persister .getEntityMetamodel ().hasImmutableNaturalId () ) {
341
+ else if ( persister .getEntityMetamodel ().hasImmutableNaturalId () ) {
343
342
// an immutable natural-id is not retrieved during a normal database-snapshot operation...
344
343
final Object naturalIdFromDb = persister .getNaturalIdentifierSnapshot ( id , session );
345
344
naturalIdResolutions .cacheResolutionFromLoad ( id , naturalIdFromDb , persister );
@@ -353,13 +352,14 @@ public Object getNaturalIdSnapshot(Object id, EntityPersister persister) throws
353
352
if ( entitySnapshot == NO_ROW || entitySnapshot == null ) {
354
353
return null ;
355
354
}
356
-
357
- final Object [] naturalIdSnapshotSubSet = new Object [ props .length ];
358
- for ( int i = 0 ; i < props .length ; i ++ ) {
359
- naturalIdSnapshotSubSet [i ] = entitySnapshot [ props [i ] ];
355
+ else {
356
+ final Object [] naturalIdSnapshotSubSet = new Object [ props .length ];
357
+ for ( int i = 0 ; i < props .length ; i ++ ) {
358
+ naturalIdSnapshotSubSet [i ] = entitySnapshot [ props [i ] ];
359
+ }
360
+ naturalIdResolutions .cacheResolutionFromLoad ( id , naturalIdSnapshotSubSet , persister );
361
+ return naturalIdSnapshotSubSet ;
360
362
}
361
- naturalIdResolutions .cacheResolutionFromLoad ( id , naturalIdSnapshotSubSet , persister );
362
- return naturalIdSnapshotSubSet ;
363
363
}
364
364
}
365
365
@@ -429,9 +429,8 @@ public boolean containsEntityHolder(EntityKey key) {
429
429
public void postLoad (JdbcValuesSourceProcessingState processingState , Consumer <EntityHolder > holderConsumer ) {
430
430
final Callback callback = processingState .getExecutionContext ().getCallback ();
431
431
if ( processingState .getLoadingEntityHolders () != null ) {
432
- final EventListenerGroup <PostLoadEventListener > listenerGroup = getSession ().getFactory ()
433
- .getFastSessionServices ()
434
- .eventListenerGroup_POST_LOAD ;
432
+ final EventListenerGroup <PostLoadEventListener > listenerGroup =
433
+ getSession ().getFactory ().getFastSessionServices ().eventListenerGroup_POST_LOAD ;
435
434
final PostLoadEvent postLoadEvent = processingState .getPostLoadEvent ();
436
435
for ( final EntityHolder holder : processingState .getLoadingEntityHolders () ) {
437
436
processLoadedEntityHolder ( holder , listenerGroup , postLoadEvent , callback , holderConsumer );
@@ -460,26 +459,27 @@ private void processLoadedEntityHolder(
460
459
// in which case we added an entry with a null proxy and entity.
461
460
// Remove that empty entry on post load to avoid unwanted side effects
462
461
entitiesByKey .remove ( holder .getEntityKey () );
463
- return ;
464
462
}
465
- if ( postLoadEvent != null ) {
466
- postLoadEvent .reset ();
467
- postLoadEvent .setEntity ( holder .getEntity () )
468
- .setId ( holder .getEntityKey ().getIdentifier () )
469
- .setPersister ( holder .getDescriptor () );
470
- listenerGroup .fireEventOnEachListener (
471
- postLoadEvent ,
472
- PostLoadEventListener ::onPostLoad
473
- );
474
- }
475
- if ( callback != null ) {
476
- callback .invokeAfterLoadActions (
477
- holder .getEntity (),
478
- holder .getDescriptor (),
479
- getSession ()
480
- );
463
+ else {
464
+ if ( postLoadEvent != null ) {
465
+ postLoadEvent .reset ();
466
+ postLoadEvent .setEntity ( holder .getEntity () )
467
+ .setId ( holder .getEntityKey ().getIdentifier () )
468
+ .setPersister ( holder .getDescriptor () );
469
+ listenerGroup .fireEventOnEachListener (
470
+ postLoadEvent ,
471
+ PostLoadEventListener ::onPostLoad
472
+ );
473
+ }
474
+ if ( callback != null ) {
475
+ callback .invokeAfterLoadActions (
476
+ holder .getEntity (),
477
+ holder .getDescriptor (),
478
+ getSession ()
479
+ );
480
+ }
481
+ ( (EntityHolderImpl ) holder ).entityInitializer = null ;
481
482
}
482
- ( (EntityHolderImpl ) holder ).entityInitializer = null ;
483
483
}
484
484
485
485
@ Override
@@ -498,7 +498,8 @@ public EntityHolder addEntityHolder(EntityKey key, Object entity) {
498
498
holder = oldHolder ;
499
499
}
500
500
else {
501
- entityHolderMap .put ( key , holder = EntityHolderImpl .forEntity ( key , key .getPersister (), entity ) );
501
+ holder = EntityHolderImpl .forEntity ( key , key .getPersister (), entity );
502
+ entityHolderMap .put ( key , holder );
502
503
}
503
504
holder .state = EntityHolderState .INITIALIZED ;
504
505
final BatchFetchQueue fetchQueue = this .batchFetchQueue ;
@@ -614,8 +615,8 @@ public EntityEntry addEntity(
614
615
final boolean existsInDatabase ,
615
616
final EntityPersister persister ,
616
617
final boolean disableVersionIncrement ) {
617
- EntityHolder entityHolder = addEntityHolder ( entityKey , entity );
618
- EntityEntry entityEntry = addEntry (
618
+ final EntityHolder entityHolder = addEntityHolder ( entityKey , entity );
619
+ final EntityEntry entityEntry = addEntry (
619
620
entity ,
620
621
status ,
621
622
loadedState ,
@@ -661,7 +662,7 @@ public EntityEntry addEntry(
661
662
removes the virtual call, and allows the methods to be inlined. In this critical code path, it has a very
662
663
large impact on performance to make virtual method calls.
663
664
*/
664
- if (persister .getEntityEntryFactory () instanceof MutableEntityEntryFactory ) {
665
+ if ( persister .getEntityEntryFactory () instanceof MutableEntityEntryFactory ) {
665
666
//noinspection RedundantCast
666
667
e = ( (MutableEntityEntryFactory ) persister .getEntityEntryFactory () ).createEntityEntry (
667
668
status ,
@@ -731,7 +732,7 @@ public boolean reassociateIfUninitializedProxy(Object value) throws MappingExcep
731
732
if ( ! Hibernate .isInitialized ( value ) ) {
732
733
733
734
// could be a proxy....
734
- final LazyInitializer lazyInitializer = HibernateProxy . extractLazyInitializer ( value );
735
+ final LazyInitializer lazyInitializer = extractLazyInitializer ( value );
735
736
if ( lazyInitializer != null ) {
736
737
reassociateProxy ( lazyInitializer , asHibernateProxy ( value ) );
737
738
return true ;
@@ -740,7 +741,8 @@ public boolean reassociateIfUninitializedProxy(Object value) throws MappingExcep
740
741
// or an uninitialized enhanced entity ("bytecode proxy")...
741
742
if ( isPersistentAttributeInterceptable ( value ) ) {
742
743
final PersistentAttributeInterceptable bytecodeProxy = asPersistentAttributeInterceptable ( value );
743
- final BytecodeLazyAttributeInterceptor interceptor = (BytecodeLazyAttributeInterceptor ) bytecodeProxy .$$_hibernate_getInterceptor ();
744
+ final BytecodeLazyAttributeInterceptor interceptor =
745
+ (BytecodeLazyAttributeInterceptor ) bytecodeProxy .$$_hibernate_getInterceptor ();
744
746
if ( interceptor != null ) {
745
747
interceptor .setSession ( getSession () );
746
748
}
@@ -754,7 +756,7 @@ public boolean reassociateIfUninitializedProxy(Object value) throws MappingExcep
754
756
755
757
@ Override
756
758
public void reassociateProxy (Object value , Object id ) throws MappingException {
757
- final LazyInitializer lazyInitializer = HibernateProxy . extractLazyInitializer ( value );
759
+ final LazyInitializer lazyInitializer = extractLazyInitializer ( value );
758
760
if ( lazyInitializer != null ) {
759
761
LOG .debugf ( "Setting proxy identifier: %s" , id );
760
762
lazyInitializer .setIdentifier ( id );
@@ -792,7 +794,7 @@ private void reassociateProxy(LazyInitializer li, HibernateProxy proxy) {
792
794
793
795
@ Override
794
796
public Object unproxy (Object maybeProxy ) throws HibernateException {
795
- final LazyInitializer lazyInitializer = HibernateProxy . extractLazyInitializer ( maybeProxy );
797
+ final LazyInitializer lazyInitializer = extractLazyInitializer ( maybeProxy );
796
798
if ( lazyInitializer != null ) {
797
799
if ( lazyInitializer .isUninitialized () ) {
798
800
throw new PersistentObjectException (
@@ -809,7 +811,7 @@ public Object unproxy(Object maybeProxy) throws HibernateException {
809
811
810
812
@ Override
811
813
public Object unproxyAndReassociate (final Object maybeProxy ) throws HibernateException {
812
- final LazyInitializer lazyInitializer = HibernateProxy . extractLazyInitializer ( maybeProxy );
814
+ final LazyInitializer lazyInitializer = extractLazyInitializer ( maybeProxy );
813
815
if ( lazyInitializer != null ) {
814
816
reassociateProxy ( lazyInitializer , asHibernateProxy ( maybeProxy ) );
815
817
//initialize + unwrap the object and return it
@@ -858,7 +860,7 @@ public Object narrowProxy(Object proxy, EntityPersister persister, EntityKey key
858
860
859
861
// Similarly, if the original HibernateProxy is initialized, there
860
862
// is again no point in creating a proxy. Just return the impl
861
- final LazyInitializer lazyInitializer = HibernateProxy . extractLazyInitializer ( proxy );
863
+ final LazyInitializer lazyInitializer = extractLazyInitializer ( proxy );
862
864
if ( !lazyInitializer .isUninitialized () ) {
863
865
final Object impl = lazyInitializer .getImplementation ();
864
866
// can we return it?
@@ -871,16 +873,13 @@ public Object narrowProxy(Object proxy, EntityPersister persister, EntityKey key
871
873
872
874
// Otherwise, create the narrowed proxy
873
875
final HibernateProxy narrowedProxy = asHibernateProxy ( persister .createProxy ( key .getIdentifier (), session ) );
874
-
875
876
// set the read-only/modifiable mode in the new proxy to what it was in the original proxy
876
- final boolean readOnlyOrig = lazyInitializer .isReadOnly ();
877
- narrowedProxy .getHibernateLazyInitializer ().setReadOnly ( readOnlyOrig );
878
-
877
+ narrowedProxy .getHibernateLazyInitializer ().setReadOnly ( lazyInitializer .isReadOnly () );
879
878
return narrowedProxy ;
880
879
}
881
880
else {
882
881
if ( object != null ) {
883
- HibernateProxy . extractLazyInitializer ( proxy ).setImplementation ( object );
882
+ extractLazyInitializer ( proxy ).setImplementation ( object );
884
883
}
885
884
return proxy ;
886
885
}
@@ -1531,7 +1530,7 @@ && isFoundInParent( propertyName, childEntity, persister, collectionPersister, p
1531
1530
if ( mergeMap != null ) {
1532
1531
for ( Object o : mergeMap .entrySet () ) {
1533
1532
final Entry <?,?> mergeMapEntry = (Entry <?,?>) o ;
1534
- final LazyInitializer lazyInitializer = HibernateProxy . extractLazyInitializer ( mergeMapEntry .getKey () );
1533
+ final LazyInitializer lazyInitializer = extractLazyInitializer ( mergeMapEntry .getKey () );
1535
1534
if ( lazyInitializer != null ) {
1536
1535
if ( persister .isSubclassEntityName ( lazyInitializer .getEntityName () ) ) {
1537
1536
HibernateProxy proxy = asHibernateProxy ( mergeMapEntry .getKey () );
@@ -1693,17 +1692,17 @@ private void clearNullProperties() {
1693
1692
@ Override
1694
1693
public boolean isReadOnly (Object entityOrProxy ) {
1695
1694
if ( entityOrProxy == null ) {
1696
- throw new AssertionFailure ( "object must be non-null. " );
1695
+ throw new IllegalArgumentException ( "Null entity instance " );
1697
1696
}
1698
1697
boolean isReadOnly ;
1699
- final LazyInitializer lazyInitializer = HibernateProxy . extractLazyInitializer ( entityOrProxy );
1698
+ final LazyInitializer lazyInitializer = extractLazyInitializer ( entityOrProxy );
1700
1699
if ( lazyInitializer != null ) {
1701
1700
isReadOnly = lazyInitializer .isReadOnly ();
1702
1701
}
1703
1702
else {
1704
1703
final EntityEntry ee = getEntry ( entityOrProxy );
1705
1704
if ( ee == null ) {
1706
- throw new TransientObjectException ( "Instance was not associated with this persistence context" );
1705
+ throw new IllegalArgumentException ( "Instance is not associated with this persistence context" );
1707
1706
}
1708
1707
isReadOnly = ee .isReadOnly ();
1709
1708
}
@@ -1713,12 +1712,12 @@ public boolean isReadOnly(Object entityOrProxy) {
1713
1712
@ Override
1714
1713
public void setReadOnly (Object object , boolean readOnly ) {
1715
1714
if ( object == null ) {
1716
- throw new AssertionFailure ( "object must be non-null. " );
1715
+ throw new IllegalArgumentException ( "Null entity instance " );
1717
1716
}
1718
1717
if ( isReadOnly ( object ) == readOnly ) {
1719
1718
return ;
1720
1719
}
1721
- final LazyInitializer lazyInitializer = HibernateProxy . extractLazyInitializer ( object );
1720
+ final LazyInitializer lazyInitializer = extractLazyInitializer ( object );
1722
1721
if ( lazyInitializer != null ) {
1723
1722
setProxyReadOnly ( lazyInitializer , readOnly );
1724
1723
if ( ! lazyInitializer .isUninitialized () ) {
@@ -1733,7 +1732,7 @@ public void setReadOnly(Object object, boolean readOnly) {
1733
1732
// PersistenceContext.proxyFor( entity ) returns entity if there is no proxy for that entity
1734
1733
// so need to check the return value to be sure it is really a proxy
1735
1734
final Object maybeProxy = getSession ().getPersistenceContextInternal ().proxyFor ( object );
1736
- final LazyInitializer lazyInitializer1 = HibernateProxy . extractLazyInitializer ( maybeProxy );
1735
+ final LazyInitializer lazyInitializer1 = extractLazyInitializer ( maybeProxy );
1737
1736
if ( lazyInitializer1 != null ) {
1738
1737
setProxyReadOnly ( lazyInitializer1 , readOnly );
1739
1738
}
@@ -1751,7 +1750,7 @@ private void setProxyReadOnly(LazyInitializer hibernateLazyInitializer, boolean
1751
1750
private void setEntityReadOnly (Object entity , boolean readOnly ) {
1752
1751
final EntityEntry entry = getEntry ( entity );
1753
1752
if ( entry == null ) {
1754
- throw new TransientObjectException ( "Instance was not associated with this persistence context" );
1753
+ throw new IllegalArgumentException ( "Instance was not associated with this persistence context" );
1755
1754
}
1756
1755
entry .setReadOnly ( readOnly , entity );
1757
1756
hasNonReadOnlyEntities = hasNonReadOnlyEntities || ! readOnly ;
@@ -1963,7 +1962,7 @@ public static StatefulPersistenceContext deserialize(
1963
1962
final EntityHolderImpl holder = EntityHolderImpl .forEntity ( ek , persister , entity );
1964
1963
holder .state = state ;
1965
1964
if ( proxy != null ) {
1966
- final LazyInitializer lazyInitializer = HibernateProxy . extractLazyInitializer ( proxy );
1965
+ final LazyInitializer lazyInitializer = extractLazyInitializer ( proxy );
1967
1966
if ( lazyInitializer != null ) {
1968
1967
lazyInitializer .setSession ( session );
1969
1968
holder .proxy = proxy ;
0 commit comments