Skip to content

Commit 302dd29

Browse files
committed
move check for remove(detached) to SessionImpl for consistency/simplicity
Signed-off-by: Gavin King <[email protected]>
1 parent f7db3f0 commit 302dd29

File tree

4 files changed

+37
-63
lines changed

4 files changed

+37
-63
lines changed

hibernate-core/src/main/java/org/hibernate/event/internal/AbstractFlushingEventListener.java

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@
3030
import org.hibernate.engine.spi.SessionImplementor;
3131
import org.hibernate.engine.spi.Status;
3232
import org.hibernate.event.service.spi.EventListenerGroup;
33-
import org.hibernate.event.service.spi.JpaBootstrapSensitive;
3433
import org.hibernate.event.spi.EventSource;
3534
import org.hibernate.event.spi.FlushEntityEvent;
3635
import org.hibernate.event.spi.FlushEntityEventListener;
@@ -50,14 +49,10 @@
5049
*
5150
* @author Steve Ebersole
5251
*/
53-
public abstract class AbstractFlushingEventListener implements JpaBootstrapSensitive {
52+
public abstract class AbstractFlushingEventListener {
5453

5554
private static final CoreMessageLogger LOG = Logger.getMessageLogger( MethodHandles.lookup(), CoreMessageLogger.class, AbstractFlushingEventListener.class.getName() );
5655

57-
@Override
58-
public void wasJpaBootstrap(boolean wasJpaBootstrap) {
59-
}
60-
6156
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
6257
// Pre-flushing section
6358
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

hibernate-core/src/main/java/org/hibernate/event/internal/DefaultDeleteEventListener.java

Lines changed: 1 addition & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
import org.hibernate.engine.spi.EntityKey;
2929
import org.hibernate.engine.spi.PersistenceContext;
3030
import org.hibernate.engine.spi.Status;
31-
import org.hibernate.event.service.spi.JpaBootstrapSensitive;
3231
import org.hibernate.event.spi.DeleteContext;
3332
import org.hibernate.event.spi.DeleteEvent;
3433
import org.hibernate.event.spi.DeleteEventListener;
@@ -60,22 +59,16 @@
6059
*
6160
* @author Steve Ebersole
6261
*/
63-
public class DefaultDeleteEventListener implements DeleteEventListener, CallbackRegistryConsumer, JpaBootstrapSensitive {
62+
public class DefaultDeleteEventListener implements DeleteEventListener, CallbackRegistryConsumer {
6463
private static final CoreMessageLogger LOG = CoreLogging.messageLogger( DefaultDeleteEventListener.class );
6564

6665
private CallbackRegistry callbackRegistry;
67-
private boolean jpaBootstrap;
6866

6967
@Override
7068
public void injectCallbackRegistry(CallbackRegistry callbackRegistry) {
7169
this.callbackRegistry = callbackRegistry;
7270
}
7371

74-
@Override
75-
public void wasJpaBootstrap(boolean wasJpaBootstrap) {
76-
this.jpaBootstrap = wasJpaBootstrap;
77-
}
78-
7972
/**
8073
* Handle the given delete event.
8174
*
@@ -173,7 +166,6 @@ private void deleteUnmanagedInstance(DeleteEvent event, DeleteContext transientE
173166
deleteTransientEntity( source, entity, persister, transientEntities );
174167
}
175168
else {
176-
performDetachedEntityDeletionCheck( event );
177169
deleteDetachedEntity( event, transientEntities, entity, persister, source );
178170
}
179171
}
@@ -315,29 +307,6 @@ private boolean hasRegisteredRemoveCallbacks(EntityPersister persister) {
315307
|| callbackRegistry.hasRegisteredCallbacks( mappedClass, CallbackType.POST_REMOVE );
316308
}
317309

318-
/**
319-
* Called when we have recognized an attempt to delete a detached entity.
320-
* <p>
321-
* This is perfectly legal in regular Hibernate usage; the JPA spec,
322-
* however, forbids it.
323-
*/
324-
protected void performDetachedEntityDeletionCheck(DeleteEvent event) {
325-
if ( jpaBootstrap ) {
326-
disallowDeletionOfDetached( event );
327-
}
328-
}
329-
330-
private void disallowDeletionOfDetached(DeleteEvent event) {
331-
final EventSource source = event.getSession();
332-
final String explicitEntityName = event.getEntityName();
333-
final EntityPersister persister = source.getEntityPersister( explicitEntityName, event.getObject() );
334-
final Object id = persister.getIdentifier( event.getObject(), source );
335-
final String entityName = explicitEntityName == null
336-
? source.guessEntityName( event.getObject() )
337-
: explicitEntityName;
338-
throw new IllegalArgumentException( "Given entity is not associated with the persistence context [" + entityName + "#" + id + "]" );
339-
}
340-
341310
/**
342311
* We encountered a delete request on a transient instance.
343312
* <p>

hibernate-core/src/main/java/org/hibernate/event/service/spi/JpaBootstrapSensitive.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,10 @@
1010
* Defines an event listener that is sensitive to whether a native or jpa bootstrap was performed
1111
*
1212
* @author Steve Ebersole
13+
*
14+
* @deprecated This is no longer implemented by any listener
1315
*/
16+
@Deprecated(since = "7")
1417
public interface JpaBootstrapSensitive {
1518
void wasJpaBootstrap(boolean wasJpaBootstrap);
1619
}

hibernate-core/src/main/java/org/hibernate/internal/SessionImpl.java

Lines changed: 32 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -354,10 +354,10 @@ protected void applyQuerySettingsAndHints(Query<?> query) {
354354
}
355355
}
356356

357-
private Object getSessionProperty(final String name) {
357+
private Object getSessionProperty(String propertyName) {
358358
return properties == null
359-
? fastSessionServices.defaultSessionProperties.get( name )
360-
: properties.get( name );
359+
? fastSessionServices.defaultSessionProperties.get( propertyName )
360+
: properties.get( propertyName );
361361
}
362362

363363
@Override
@@ -464,9 +464,7 @@ protected boolean shouldCloseJdbcCoordinatorOnClose(boolean isTransactionCoordin
464464

465465
final ActionQueue actionQueue = getActionQueue();
466466
if ( actionQueue.hasBeforeTransactionActions() || actionQueue.hasAfterTransactionActions() ) {
467-
log.warn(
468-
"On close, shared Session had before/after transaction actions that have not yet been processed"
469-
);
467+
log.warn( "On close, shared Session had before/after transaction actions that have not yet been processed" );
470468
}
471469
return false;
472470
}
@@ -575,11 +573,8 @@ public LockMode getCurrentLockMode(Object object) throws HibernateException {
575573
}
576574

577575
if ( e.getStatus().isDeletedOrGone() ) {
578-
throw new ObjectDeletedException(
579-
"The given object was deleted",
580-
e.getId(),
581-
e.getPersister().getEntityName()
582-
);
576+
throw new ObjectDeletedException( "The given object was deleted", e.getId(),
577+
e.getPersister().getEntityName() );
583578
}
584579

585580
return e.getLockMode();
@@ -895,6 +890,7 @@ private void logRemoveOrphanBeforeUpdates(String timing, String entityName, Obje
895890
}
896891

897892
private void fireDelete(final DeleteEvent event) {
893+
checkEntityManagedIfJpa( event.getEntityName(), event.getObject() );
898894
try {
899895
pulseTransactionCoordinator();
900896
fastSessionServices.eventListenerGroup_DELETE
@@ -916,6 +912,7 @@ private void fireDelete(final DeleteEvent event) {
916912
}
917913

918914
private void fireDelete(final DeleteEvent event, final DeleteContext transientEntities) {
915+
checkEntityManagedIfJpa( event.getEntityName(), event.getObject() );
919916
try {
920917
pulseTransactionCoordinator();
921918
fastSessionServices.eventListenerGroup_DELETE
@@ -1204,8 +1201,17 @@ private void fireRefresh(final RefreshContext refreshedAlready, final RefreshEve
12041201
private void checkEntityManaged(String entityName, Object entity) {
12051202
if ( !getSessionFactory().getSessionFactoryOptions().isAllowRefreshDetachedEntity() ) {
12061203
if ( !managed( entityName, entity ) ) {
1207-
throw new IllegalArgumentException(
1208-
"Given entity is not associated with the persistence context" );
1204+
throw new IllegalArgumentException( "Given entity is not associated with the persistence context" );
1205+
}
1206+
}
1207+
}
1208+
1209+
private void checkEntityManagedIfJpa(String entityName, Object entity) {
1210+
if ( getSessionFactory().getSessionFactoryOptions().isJpaBootstrap() ) {
1211+
if ( !managed( entityName, entity )
1212+
// just in case it was already deleted
1213+
&& !persistenceContext.isEntryFor( entity ) ) {
1214+
throw new IllegalArgumentException( "Given entity is not associated with the persistence context" );
12091215
}
12101216
}
12111217
}
@@ -1423,8 +1429,9 @@ public Object getIdentifier(Object object) throws HibernateException {
14231429
}
14241430

14251431
/**
1426-
* Get the id value for an object that is actually associated with the session. This
1427-
* is a bit stricter than getEntityIdentifierIfNotUnsaved().
1432+
* Get the id value for an object that is actually associated with the session.
1433+
* This is a bit stricter than
1434+
* {@link org.hibernate.engine.internal.ForeignKeys#getEntityIdentifierIfNotUnsaved}.
14281435
*/
14291436
@Override
14301437
public Object getContextEntityIdentifier(Object object) {
@@ -1457,20 +1464,20 @@ public boolean contains(Object object) {
14571464
try {
14581465
final LazyInitializer lazyInitializer = extractLazyInitializer( object );
14591466
if ( lazyInitializer != null ) {
1460-
//do not use proxiesByKey, since not all
1461-
//proxies that point to this session's
1462-
//instances are in that collection!
1467+
// don't use proxiesByKey, since not all
1468+
// proxies that point to this session's
1469+
// instances are in that collection!
14631470
if ( lazyInitializer.isUninitialized() ) {
1464-
//if it is an uninitialized proxy, pointing
1465-
//with this session, then when it is accessed,
1466-
//the underlying instance will be "contained"
1471+
// if it's an uninitialized proxy associated
1472+
// with this session, then when it is accessed,
1473+
// the underlying instance will be "contained"
14671474
return lazyInitializer.getSession() == this;
14681475
}
14691476
else {
1470-
//if it is initialized, see if the underlying
1471-
//instance is contained, since we need to
1472-
//account for the fact that it might have been
1473-
//evicted
1477+
// if it's initialized, see if the underlying
1478+
// instance is contained, since we need to
1479+
// account for the fact that it might have been
1480+
// evicted
14741481
object = lazyInitializer.getImplementation();
14751482
}
14761483
}

0 commit comments

Comments
 (0)