diff --git a/hibernate-core/src/main/java/org/hibernate/engine/internal/ForeignKeys.java b/hibernate-core/src/main/java/org/hibernate/engine/internal/ForeignKeys.java
index 608b1b8f46..cdb07cb2d1 100644
--- a/hibernate-core/src/main/java/org/hibernate/engine/internal/ForeignKeys.java
+++ b/hibernate-core/src/main/java/org/hibernate/engine/internal/ForeignKeys.java
@@ -373,10 +373,9 @@ public final class ForeignKeys {
private static void throwIfTransient(String entityName, Object object, SharedSessionContractImplementor session) {
if ( isTransient( entityName, object, Boolean.FALSE, session ) ) {
- throw new TransientObjectException(
- "object references an unsaved transient instance - save the transient instance before flushing: " +
- (entityName == null ? session.guessEntityName(object) : entityName)
- );
+ throw new TransientObjectException( "Entity references an unsaved transient instance of '"
+ + (entityName == null ? session.guessEntityName(object) : entityName)
+ + "' (make the transient instance persistent before flushing)" );
}
}
diff --git a/hibernate-core/src/main/java/org/hibernate/engine/internal/StatefulPersistenceContext.java b/hibernate-core/src/main/java/org/hibernate/engine/internal/StatefulPersistenceContext.java
index 026b632e12..4ba40243e9 100644
--- a/hibernate-core/src/main/java/org/hibernate/engine/internal/StatefulPersistenceContext.java
+++ b/hibernate-core/src/main/java/org/hibernate/engine/internal/StatefulPersistenceContext.java
@@ -33,7 +33,6 @@ import org.hibernate.LockMode;
import org.hibernate.MappingException;
import org.hibernate.NonUniqueObjectException;
import org.hibernate.PersistentObjectException;
-import org.hibernate.TransientObjectException;
import org.hibernate.bytecode.enhance.spi.interceptor.BytecodeLazyAttributeInterceptor;
import org.hibernate.bytecode.enhance.spi.interceptor.EnhancementAsProxyLazinessInterceptor;
import org.hibernate.collection.spi.PersistentCollection;
@@ -79,6 +78,7 @@ import static org.hibernate.engine.internal.ManagedTypeHelper.asHibernateProxy;
import static org.hibernate.engine.internal.ManagedTypeHelper.asManagedEntity;
import static org.hibernate.engine.internal.ManagedTypeHelper.asPersistentAttributeInterceptable;
import static org.hibernate.engine.internal.ManagedTypeHelper.isPersistentAttributeInterceptable;
+import static org.hibernate.proxy.HibernateProxy.extractLazyInitializer;
/**
* A stateful implementation of the {@link PersistenceContext} contract, meaning that we maintain this
@@ -217,7 +217,7 @@ public class StatefulPersistenceContext implements PersistenceContext {
//
@Override
public PersistentCollection> useUnownedCollection(CollectionKey key) {
- return ( unownedCollections == null ) ? null : unownedCollections.remove( key );
+ return unownedCollections == null ? null : unownedCollections.remove( key );
}
@Override
@@ -234,7 +234,7 @@ public class StatefulPersistenceContext implements PersistenceContext {
//Strictly avoid lambdas in this case
for ( EntityHolderImpl value : entitiesByKey.values() ) {
if ( value != null && value.proxy != null ) {
- HibernateProxy.extractLazyInitializer( value.proxy ).unsetSession();
+ extractLazyInitializer( value.proxy ).unsetSession();
}
}
}
@@ -337,9 +337,8 @@ public class StatefulPersistenceContext implements PersistenceContext {
if ( cachedValue != null ) {
return cachedValue;
}
-
// check to see if the natural id is mutable/immutable
- if ( persister.getEntityMetamodel().hasImmutableNaturalId() ) {
+ else if ( persister.getEntityMetamodel().hasImmutableNaturalId() ) {
// an immutable natural-id is not retrieved during a normal database-snapshot operation...
final Object naturalIdFromDb = persister.getNaturalIdentifierSnapshot( id, session );
naturalIdResolutions.cacheResolutionFromLoad( id, naturalIdFromDb, persister );
@@ -353,13 +352,14 @@ public class StatefulPersistenceContext implements PersistenceContext {
if ( entitySnapshot == NO_ROW || entitySnapshot == null ) {
return null;
}
-
- final Object[] naturalIdSnapshotSubSet = new Object[ props.length ];
- for ( int i = 0; i < props.length; i++ ) {
- naturalIdSnapshotSubSet[i] = entitySnapshot[ props[i] ];
+ else {
+ final Object[] naturalIdSnapshotSubSet = new Object[ props.length ];
+ for ( int i = 0; i < props.length; i++ ) {
+ naturalIdSnapshotSubSet[i] = entitySnapshot[ props[i] ];
+ }
+ naturalIdResolutions.cacheResolutionFromLoad( id, naturalIdSnapshotSubSet, persister );
+ return naturalIdSnapshotSubSet;
}
- naturalIdResolutions.cacheResolutionFromLoad( id, naturalIdSnapshotSubSet, persister );
- return naturalIdSnapshotSubSet;
}
}
@@ -429,9 +429,8 @@ public class StatefulPersistenceContext implements PersistenceContext {
public void postLoad(JdbcValuesSourceProcessingState processingState, Consumer holderConsumer) {
final Callback callback = processingState.getExecutionContext().getCallback();
if ( processingState.getLoadingEntityHolders() != null ) {
- final EventListenerGroup listenerGroup = getSession().getFactory()
- .getFastSessionServices()
- .eventListenerGroup_POST_LOAD;
+ final EventListenerGroup listenerGroup =
+ getSession().getFactory().getFastSessionServices().eventListenerGroup_POST_LOAD;
final PostLoadEvent postLoadEvent = processingState.getPostLoadEvent();
for ( final EntityHolder holder : processingState.getLoadingEntityHolders() ) {
processLoadedEntityHolder( holder, listenerGroup, postLoadEvent, callback, holderConsumer );
@@ -460,26 +459,27 @@ public class StatefulPersistenceContext implements PersistenceContext {
// in which case we added an entry with a null proxy and entity.
// Remove that empty entry on post load to avoid unwanted side effects
entitiesByKey.remove( holder.getEntityKey() );
- return;
}
- if ( postLoadEvent != null ) {
- postLoadEvent.reset();
- postLoadEvent.setEntity( holder.getEntity() )
- .setId( holder.getEntityKey().getIdentifier() )
- .setPersister( holder.getDescriptor() );
- listenerGroup.fireEventOnEachListener(
- postLoadEvent,
- PostLoadEventListener::onPostLoad
- );
+ else {
+ if ( postLoadEvent != null ) {
+ postLoadEvent.reset();
+ postLoadEvent.setEntity( holder.getEntity() )
+ .setId( holder.getEntityKey().getIdentifier() )
+ .setPersister( holder.getDescriptor() );
+ listenerGroup.fireEventOnEachListener(
+ postLoadEvent,
+ PostLoadEventListener::onPostLoad
+ );
+ }
+ if ( callback != null ) {
+ callback.invokeAfterLoadActions(
+ holder.getEntity(),
+ holder.getDescriptor(),
+ getSession()
+ );
+ }
+ ( (EntityHolderImpl) holder ).entityInitializer = null;
}
- if ( callback != null ) {
- callback.invokeAfterLoadActions(
- holder.getEntity(),
- holder.getDescriptor(),
- getSession()
- );
- }
- ( (EntityHolderImpl) holder ).entityInitializer = null;
}
@Override
@@ -498,7 +498,8 @@ public class StatefulPersistenceContext implements PersistenceContext {
holder = oldHolder;
}
else {
- entityHolderMap.put( key, holder = EntityHolderImpl.forEntity( key, key.getPersister(), entity ) );
+ holder = EntityHolderImpl.forEntity( key, key.getPersister(), entity );
+ entityHolderMap.put( key, holder );
}
holder.state = EntityHolderState.INITIALIZED;
final BatchFetchQueue fetchQueue = this.batchFetchQueue;
@@ -614,8 +615,8 @@ public class StatefulPersistenceContext implements PersistenceContext {
final boolean existsInDatabase,
final EntityPersister persister,
final boolean disableVersionIncrement) {
- EntityHolder entityHolder = addEntityHolder( entityKey, entity );
- EntityEntry entityEntry = addEntry(
+ final EntityHolder entityHolder = addEntityHolder( entityKey, entity );
+ final EntityEntry entityEntry = addEntry(
entity,
status,
loadedState,
@@ -661,7 +662,7 @@ public class StatefulPersistenceContext implements PersistenceContext {
removes the virtual call, and allows the methods to be inlined. In this critical code path, it has a very
large impact on performance to make virtual method calls.
*/
- if (persister.getEntityEntryFactory() instanceof MutableEntityEntryFactory) {
+ if ( persister.getEntityEntryFactory() instanceof MutableEntityEntryFactory ) {
//noinspection RedundantCast
e = ( (MutableEntityEntryFactory) persister.getEntityEntryFactory() ).createEntityEntry(
status,
@@ -731,7 +732,7 @@ public class StatefulPersistenceContext implements PersistenceContext {
if ( ! Hibernate.isInitialized( value ) ) {
// could be a proxy....
- final LazyInitializer lazyInitializer = HibernateProxy.extractLazyInitializer( value );
+ final LazyInitializer lazyInitializer = extractLazyInitializer( value );
if ( lazyInitializer != null ) {
reassociateProxy( lazyInitializer, asHibernateProxy( value ) );
return true;
@@ -740,7 +741,8 @@ public class StatefulPersistenceContext implements PersistenceContext {
// or an uninitialized enhanced entity ("bytecode proxy")...
if ( isPersistentAttributeInterceptable( value ) ) {
final PersistentAttributeInterceptable bytecodeProxy = asPersistentAttributeInterceptable( value );
- final BytecodeLazyAttributeInterceptor interceptor = (BytecodeLazyAttributeInterceptor) bytecodeProxy.$$_hibernate_getInterceptor();
+ final BytecodeLazyAttributeInterceptor interceptor =
+ (BytecodeLazyAttributeInterceptor) bytecodeProxy.$$_hibernate_getInterceptor();
if ( interceptor != null ) {
interceptor.setSession( getSession() );
}
@@ -754,7 +756,7 @@ public class StatefulPersistenceContext implements PersistenceContext {
@Override
public void reassociateProxy(Object value, Object id) throws MappingException {
- final LazyInitializer lazyInitializer = HibernateProxy.extractLazyInitializer( value );
+ final LazyInitializer lazyInitializer = extractLazyInitializer( value );
if ( lazyInitializer != null ) {
LOG.debugf( "Setting proxy identifier: %s", id );
lazyInitializer.setIdentifier( id );
@@ -792,7 +794,7 @@ public class StatefulPersistenceContext implements PersistenceContext {
@Override
public Object unproxy(Object maybeProxy) throws HibernateException {
- final LazyInitializer lazyInitializer = HibernateProxy.extractLazyInitializer( maybeProxy );
+ final LazyInitializer lazyInitializer = extractLazyInitializer( maybeProxy );
if ( lazyInitializer != null ) {
if ( lazyInitializer.isUninitialized() ) {
throw new PersistentObjectException(
@@ -809,7 +811,7 @@ public class StatefulPersistenceContext implements PersistenceContext {
@Override
public Object unproxyAndReassociate(final Object maybeProxy) throws HibernateException {
- final LazyInitializer lazyInitializer = HibernateProxy.extractLazyInitializer( maybeProxy );
+ final LazyInitializer lazyInitializer = extractLazyInitializer( maybeProxy );
if ( lazyInitializer != null ) {
reassociateProxy( lazyInitializer, asHibernateProxy( maybeProxy ) );
//initialize + unwrap the object and return it
@@ -858,7 +860,7 @@ public class StatefulPersistenceContext implements PersistenceContext {
// Similarly, if the original HibernateProxy is initialized, there
// is again no point in creating a proxy. Just return the impl
- final LazyInitializer lazyInitializer = HibernateProxy.extractLazyInitializer( proxy );
+ final LazyInitializer lazyInitializer = extractLazyInitializer( proxy );
if ( !lazyInitializer.isUninitialized() ) {
final Object impl = lazyInitializer.getImplementation();
// can we return it?
@@ -871,16 +873,13 @@ public class StatefulPersistenceContext implements PersistenceContext {
// Otherwise, create the narrowed proxy
final HibernateProxy narrowedProxy = asHibernateProxy( persister.createProxy( key.getIdentifier(), session ) );
-
// set the read-only/modifiable mode in the new proxy to what it was in the original proxy
- final boolean readOnlyOrig = lazyInitializer.isReadOnly();
- narrowedProxy.getHibernateLazyInitializer().setReadOnly( readOnlyOrig );
-
+ narrowedProxy.getHibernateLazyInitializer().setReadOnly( lazyInitializer.isReadOnly() );
return narrowedProxy;
}
else {
if ( object != null ) {
- HibernateProxy.extractLazyInitializer( proxy ).setImplementation( object );
+ extractLazyInitializer( proxy ).setImplementation( object );
}
return proxy;
}
@@ -1531,7 +1530,7 @@ public class StatefulPersistenceContext implements PersistenceContext {
if ( mergeMap != null ) {
for ( Object o : mergeMap.entrySet() ) {
final Entry,?> mergeMapEntry = (Entry,?>) o;
- final LazyInitializer lazyInitializer = HibernateProxy.extractLazyInitializer( mergeMapEntry.getKey() );
+ final LazyInitializer lazyInitializer = extractLazyInitializer( mergeMapEntry.getKey() );
if ( lazyInitializer != null ) {
if ( persister.isSubclassEntityName( lazyInitializer.getEntityName() ) ) {
HibernateProxy proxy = asHibernateProxy( mergeMapEntry.getKey() );
@@ -1693,17 +1692,17 @@ public class StatefulPersistenceContext implements PersistenceContext {
@Override
public boolean isReadOnly(Object entityOrProxy) {
if ( entityOrProxy == null ) {
- throw new AssertionFailure( "object must be non-null." );
+ throw new IllegalArgumentException( "Null entity instance" );
}
boolean isReadOnly;
- final LazyInitializer lazyInitializer = HibernateProxy.extractLazyInitializer( entityOrProxy );
+ final LazyInitializer lazyInitializer = extractLazyInitializer( entityOrProxy );
if ( lazyInitializer != null ) {
isReadOnly = lazyInitializer.isReadOnly();
}
else {
final EntityEntry ee = getEntry( entityOrProxy );
if ( ee == null ) {
- throw new TransientObjectException( "Instance was not associated with this persistence context" );
+ throw new IllegalArgumentException( "Instance is not associated with this persistence context" );
}
isReadOnly = ee.isReadOnly();
}
@@ -1713,12 +1712,12 @@ public class StatefulPersistenceContext implements PersistenceContext {
@Override
public void setReadOnly(Object object, boolean readOnly) {
if ( object == null ) {
- throw new AssertionFailure( "object must be non-null." );
+ throw new IllegalArgumentException( "Null entity instance" );
}
if ( isReadOnly( object ) == readOnly ) {
return;
}
- final LazyInitializer lazyInitializer = HibernateProxy.extractLazyInitializer( object );
+ final LazyInitializer lazyInitializer = extractLazyInitializer( object );
if ( lazyInitializer != null ) {
setProxyReadOnly( lazyInitializer, readOnly );
if ( ! lazyInitializer.isUninitialized() ) {
@@ -1733,7 +1732,7 @@ public class StatefulPersistenceContext implements PersistenceContext {
// PersistenceContext.proxyFor( entity ) returns entity if there is no proxy for that entity
// so need to check the return value to be sure it is really a proxy
final Object maybeProxy = getSession().getPersistenceContextInternal().proxyFor( object );
- final LazyInitializer lazyInitializer1 = HibernateProxy.extractLazyInitializer( maybeProxy );
+ final LazyInitializer lazyInitializer1 = extractLazyInitializer( maybeProxy );
if ( lazyInitializer1 != null ) {
setProxyReadOnly( lazyInitializer1, readOnly );
}
@@ -1751,7 +1750,7 @@ public class StatefulPersistenceContext implements PersistenceContext {
private void setEntityReadOnly(Object entity, boolean readOnly) {
final EntityEntry entry = getEntry( entity );
if ( entry == null ) {
- throw new TransientObjectException( "Instance was not associated with this persistence context" );
+ throw new IllegalArgumentException( "Instance was not associated with this persistence context" );
}
entry.setReadOnly( readOnly, entity );
hasNonReadOnlyEntities = hasNonReadOnlyEntities || ! readOnly;
@@ -1963,7 +1962,7 @@ public class StatefulPersistenceContext implements PersistenceContext {
final EntityHolderImpl holder = EntityHolderImpl.forEntity( ek, persister, entity );
holder.state = state;
if ( proxy != null ) {
- final LazyInitializer lazyInitializer = HibernateProxy.extractLazyInitializer( proxy );
+ final LazyInitializer lazyInitializer = extractLazyInitializer( proxy );
if ( lazyInitializer != null ) {
lazyInitializer.setSession( session );
holder.proxy = proxy;
diff --git a/hibernate-core/src/main/java/org/hibernate/engine/spi/ActionQueue.java b/hibernate-core/src/main/java/org/hibernate/engine/spi/ActionQueue.java
index 9922ac4c17..a8fbe3b2c4 100644
--- a/hibernate-core/src/main/java/org/hibernate/engine/spi/ActionQueue.java
+++ b/hibernate-core/src/main/java/org/hibernate/engine/spi/ActionQueue.java
@@ -502,8 +502,8 @@ public class ActionQueue {
final String path = transientEntities.getNonNullableTransientPropertyPaths(transientEntity).iterator().next();
//TODO: should be TransientPropertyValueException
throw new TransientObjectException( "Persistent instance of '" + insertAction.getEntityName()
- + "' with id '" + insertAction.getId()
- + "' references an unsaved transient instance via attribute '" + path
+ + "' with id [" + insertAction.getId()
+ + "] references an unsaved transient instance via attribute '" + path
+ "' (save the transient instance before flushing)" );
}
diff --git a/hibernate-core/src/main/java/org/hibernate/engine/spi/CascadingActions.java b/hibernate-core/src/main/java/org/hibernate/engine/spi/CascadingActions.java
index c655f93362..99b778c27c 100644
--- a/hibernate-core/src/main/java/org/hibernate/engine/spi/CascadingActions.java
+++ b/hibernate-core/src/main/java/org/hibernate/engine/spi/CascadingActions.java
@@ -339,7 +339,7 @@ public class CascadingActions {
boolean isCascadeDeleteEnabled)
throws HibernateException {
if ( child != null && isChildTransient( session, child, entityName ) ) {
- throw new TransientObjectException( "persistent instance references an unsaved transient instance of '"
+ throw new TransientObjectException( "Persistent instance references an unsaved transient instance of '"
+ entityName + "' (save the transient instance before flushing)" );
//TODO: should be TransientPropertyValueException
// throw new TransientPropertyValueException(
diff --git a/hibernate-core/src/main/java/org/hibernate/event/internal/DefaultDeleteEventListener.java b/hibernate-core/src/main/java/org/hibernate/event/internal/DefaultDeleteEventListener.java
index aa74c2a503..81a5c980d7 100644
--- a/hibernate-core/src/main/java/org/hibernate/event/internal/DefaultDeleteEventListener.java
+++ b/hibernate-core/src/main/java/org/hibernate/event/internal/DefaultDeleteEventListener.java
@@ -179,7 +179,8 @@ public class DefaultDeleteEventListener implements DeleteEventListener, Callback
final Object id = persister.getIdentifier( entity, source );
if ( id == null ) {
- throw new TransientObjectException("the detached instance passed to delete() had a null identifier");
+ throw new TransientObjectException( "Cannot delete instance of entity '" + persister.getEntityName()
+ + "' because it has a null identifier" );
}
final PersistenceContext persistenceContext = source.getPersistenceContextInternal();
diff --git a/hibernate-core/src/main/java/org/hibernate/event/internal/DefaultLockEventListener.java b/hibernate-core/src/main/java/org/hibernate/event/internal/DefaultLockEventListener.java
index 68844bd0e6..2071d000d9 100644
--- a/hibernate-core/src/main/java/org/hibernate/event/internal/DefaultLockEventListener.java
+++ b/hibernate-core/src/main/java/org/hibernate/event/internal/DefaultLockEventListener.java
@@ -71,8 +71,8 @@ public class DefaultLockEventListener extends AbstractLockUpgradeEventListener i
final EntityPersister persister = source.getEntityPersister( event.getEntityName(), entity );
final Object id = persister.getIdentifier( entity, source );
if ( !ForeignKeys.isNotTransient( event.getEntityName(), entity, Boolean.FALSE, source ) ) {
- throw new TransientObjectException( "cannot lock an unsaved transient instance: "
- + persister.getEntityName() );
+ throw new TransientObjectException( "Cannot lock unsaved transient instance of entity '"
+ + persister.getEntityName() + "'" );
}
entry = reassociate( event, entity, id, persister );
cascadeOnLock( event, persister, entity );
diff --git a/hibernate-core/src/main/java/org/hibernate/event/internal/DefaultRefreshEventListener.java b/hibernate-core/src/main/java/org/hibernate/event/internal/DefaultRefreshEventListener.java
index f5e455ba34..341ae7144a 100644
--- a/hibernate-core/src/main/java/org/hibernate/event/internal/DefaultRefreshEventListener.java
+++ b/hibernate-core/src/main/java/org/hibernate/event/internal/DefaultRefreshEventListener.java
@@ -20,7 +20,6 @@ import org.hibernate.engine.internal.CascadePoint;
import org.hibernate.engine.spi.ActionQueue;
import org.hibernate.engine.spi.CascadingActions;
import org.hibernate.engine.spi.EntityEntry;
-import org.hibernate.engine.spi.EntityKey;
import org.hibernate.engine.spi.PersistenceContext;
import org.hibernate.engine.spi.SessionFactoryImplementor;
import org.hibernate.event.spi.EventSource;
@@ -132,7 +131,8 @@ public class DefaultRefreshEventListener implements RefreshEventListener {
persister = source.getEntityPersister( event.getEntityName(), object );
id = persister.getIdentifier( object, event.getSession() );
if ( id == null ) {
- throw new TransientObjectException( "transient instance passed to refresh");
+ throw new TransientObjectException( "Cannot refresh instance of entity '" + persister.getEntityName()
+ + "' because it has a null identifier" );
}
if ( LOG.isTraceEnabled() ) {
LOG.tracev(
diff --git a/hibernate-core/src/main/java/org/hibernate/event/internal/DefaultReplicateEventListener.java b/hibernate-core/src/main/java/org/hibernate/event/internal/DefaultReplicateEventListener.java
index 979a6c24ec..b219a315b5 100644
--- a/hibernate-core/src/main/java/org/hibernate/event/internal/DefaultReplicateEventListener.java
+++ b/hibernate-core/src/main/java/org/hibernate/event/internal/DefaultReplicateEventListener.java
@@ -77,7 +77,8 @@ public class DefaultReplicateEventListener
// except null (that is, even ids which look like they're unsaved)
final Object id = persister.getIdentifier( entity, source );
if ( id == null ) {
- throw new TransientObjectException( "instance with null id passed to replicate()" );
+ throw new TransientObjectException( "Cannot replicate instance of entity '" + persister.getEntityName()
+ + "' because it has a null identifier" );
}
final Object oldVersion = replicationMode == ReplicationMode.EXCEPTION
diff --git a/hibernate-core/src/main/java/org/hibernate/internal/SessionImpl.java b/hibernate-core/src/main/java/org/hibernate/internal/SessionImpl.java
index ad373d36d0..62ae3c3498 100644
--- a/hibernate-core/src/main/java/org/hibernate/internal/SessionImpl.java
+++ b/hibernate-core/src/main/java/org/hibernate/internal/SessionImpl.java
@@ -572,7 +572,7 @@ public class SessionImpl
final EntityEntry e = persistenceContext.getEntry( object );
if ( e == null ) {
- throw new TransientObjectException( "Given object not associated with the session" );
+ throw new IllegalArgumentException( "Given entity is not associated with the persistence context" );
}
if ( e.getStatus().isDeletedOrGone() ) {
@@ -1419,14 +1419,14 @@ public class SessionImpl
final LazyInitializer lazyInitializer = extractLazyInitializer( object );
if ( lazyInitializer != null ) {
if ( lazyInitializer.getSession() != this ) {
- throw new TransientObjectException( "The proxy was not associated with this session" );
+ throw new IllegalArgumentException( "Given proxy is not associated with the persistence context" );
}
return lazyInitializer.getInternalIdentifier();
}
else {
final EntityEntry entry = persistenceContext.getEntry( object );
if ( entry == null ) {
- throw new TransientObjectException( "The instance was not associated with this session" );
+ throw new IllegalArgumentException( "Given entity is not associated with the persistence context" );
}
return entry.getId();
}
@@ -1682,14 +1682,14 @@ public class SessionImpl
final LazyInitializer lazyInitializer = extractLazyInitializer( object );
if ( lazyInitializer != null ) {
if ( !persistenceContext.containsProxy( object ) ) {
- throw new TransientObjectException( "proxy was not associated with the session" );
+ throw new IllegalArgumentException( "Given proxy is not associated with the persistence context" );
}
object = lazyInitializer.getImplementation();
}
final EntityEntry entry = persistenceContext.getEntry( object );
if ( entry == null ) {
- throwTransientObjectException( object );
+ throw new IllegalArgumentException( "Given entity is not associated with the persistence context" );
}
return entry.getPersister().getEntityName();
}
@@ -1707,13 +1707,6 @@ public class SessionImpl
}
}
- private void throwTransientObjectException(Object object) throws HibernateException {
- throw new TransientObjectException(
- "object references an unsaved transient instance - save the transient instance before flushing: " +
- guessEntityName( object )
- );
- }
-
@Override
public String guessEntityName(Object object) throws HibernateException {
checkOpenOrWaitingForAutoClose();
diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/mapping/EntityIdentifierMapping.java b/hibernate-core/src/main/java/org/hibernate/metamodel/mapping/EntityIdentifierMapping.java
index f6612255e8..cd764828c9 100644
--- a/hibernate-core/src/main/java/org/hibernate/metamodel/mapping/EntityIdentifierMapping.java
+++ b/hibernate-core/src/main/java/org/hibernate/metamodel/mapping/EntityIdentifierMapping.java
@@ -101,10 +101,9 @@ public interface EntityIdentifierMapping extends ValuedModelPart {
// deeper checks...
final String entityName = findContainingEntityMapping().getEntityName();
if ( ForeignKeys.isTransient( entityName, entity, Boolean.FALSE, session ) ) {
- throw new TransientObjectException(
- "object references an unsaved transient instance - save the transient instance before flushing: " +
- (entityName == null ? session.guessEntityName( entity ) : entityName)
- );
+ throw new TransientObjectException( "object references an unsaved transient instance of '"
+ + (entityName == null ? session.guessEntityName( entity ) : entityName)
+ + "' save the transient instance before flushing" );
}
id = getIdentifier( entity );
}
diff --git a/hibernate-core/src/main/java/org/hibernate/proxy/AbstractLazyInitializer.java b/hibernate-core/src/main/java/org/hibernate/proxy/AbstractLazyInitializer.java
index 5556985da4..71ca0665d9 100644
--- a/hibernate-core/src/main/java/org/hibernate/proxy/AbstractLazyInitializer.java
+++ b/hibernate-core/src/main/java/org/hibernate/proxy/AbstractLazyInitializer.java
@@ -10,7 +10,6 @@ import org.hibernate.FlushMode;
import org.hibernate.HibernateException;
import org.hibernate.LazyInitializationException;
import org.hibernate.SessionException;
-import org.hibernate.TransientObjectException;
import org.hibernate.boot.spi.SessionFactoryOptions;
import org.hibernate.engine.spi.EntityKey;
import org.hibernate.engine.spi.PersistenceContext;
@@ -372,7 +371,7 @@ public abstract class AbstractLazyInitializer implements LazyInitializer {
private void errorIfReadOnlySettingNotAvailable() {
if ( session == null ) {
- throw new TransientObjectException(
+ throw new IllegalStateException(
"Proxy for [" + entityName + "#" + id + "] is not associated with a session"
+ " (the read-only/modifiable setting is only accessible when the proxy is associated with an open session)"
);
diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/LazyOneToOneRemoveFlushAccessTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/LazyOneToOneRemoveFlushAccessTest.java
index 4ce8ec1734..7dde063a90 100644
--- a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/LazyOneToOneRemoveFlushAccessTest.java
+++ b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/LazyOneToOneRemoveFlushAccessTest.java
@@ -77,7 +77,7 @@ public class LazyOneToOneRemoveFlushAccessTest {
catch (Exception e) {
if ( shouldThrow ) {
assertThat( e.getCause() ).isInstanceOf( TransientObjectException.class )
- .hasMessageContaining( "persistent instance references an unsaved transient instance" );
+ .hasMessageContaining( "references an unsaved transient instance" );
}
else {
fail( "Test should work with transient strictness disabled, instead threw", e );
diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/readonly/ReadOnlyProxyTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/readonly/ReadOnlyProxyTest.java
index af7c4d8c8e..22a00b0a05 100644
--- a/hibernate-core/src/test/java/org/hibernate/orm/test/readonly/ReadOnlyProxyTest.java
+++ b/hibernate-core/src/test/java/org/hibernate/orm/test/readonly/ReadOnlyProxyTest.java
@@ -1285,7 +1285,7 @@ public class ReadOnlyProxyTest extends AbstractReadOnlyTest {
s.setReadOnly( dpLoaded, true );
checkReadOnly( s, dpLoaded, true );
assertFalse( Hibernate.isInitialized( dpLoaded ) );
- DataPoint dpMerged = (DataPoint) s.merge( dp );
+ DataPoint dpMerged = s.merge( dp );
assertSame( dpLoaded, dpMerged );
assertTrue( Hibernate.isInitialized( dpLoaded ) );
assertEquals( "changed", dpLoaded.getDescription() );
@@ -1336,7 +1336,7 @@ public class ReadOnlyProxyTest extends AbstractReadOnlyTest {
checkReadOnly( s, dpLoaded, false );
s.setReadOnly( dpLoaded, true );
checkReadOnly( s, dpLoaded, true );
- DataPoint dpMerged = (DataPoint) s.merge( dp );
+ DataPoint dpMerged = s.merge( dp );
assertSame( dpLoaded, dpMerged );
assertEquals( "changed", dpLoaded.getDescription() );
checkReadOnly( s, dpLoaded, true );
@@ -1385,7 +1385,7 @@ public class ReadOnlyProxyTest extends AbstractReadOnlyTest {
s.setReadOnly( dpLoaded, true );
checkReadOnly( s, dpLoaded, true );
assertFalse( Hibernate.isInitialized( dpLoaded ) );
- DataPoint dpMerged = (DataPoint) s.merge( dpEntity );
+ DataPoint dpMerged = s.merge( dpEntity );
assertSame( dpLoaded, dpMerged );
assertTrue( Hibernate.isInitialized( dpLoaded ) );
assertEquals( "changed", dpLoaded.getDescription() );
@@ -1437,7 +1437,7 @@ public class ReadOnlyProxyTest extends AbstractReadOnlyTest {
checkReadOnly( s, dpLoaded, false );
s.setReadOnly( dpLoaded, true );
checkReadOnly( s, dpLoaded, true );
- DataPoint dpMerged = (DataPoint) s.merge( dpEntity );
+ DataPoint dpMerged = s.merge( dpEntity );
assertSame( dpLoaded, dpMerged );
assertEquals( "changed", dpLoaded.getDescription() );
checkReadOnly( s, dpLoaded, true );
@@ -1484,7 +1484,7 @@ public class ReadOnlyProxyTest extends AbstractReadOnlyTest {
assertFalse( s.isReadOnly( dpEntity ) );
s.setReadOnly( dpEntity, true );
assertTrue( s.isReadOnly( dpEntity ) );
- DataPoint dpMerged = (DataPoint) s.merge( dp );
+ DataPoint dpMerged = s.merge( dp );
assertSame( dpEntity, dpMerged );
assertEquals( "changed", dpEntity.getDescription() );
assertTrue( s.isReadOnly( dpEntity ) );
@@ -1695,7 +1695,7 @@ public class ReadOnlyProxyTest extends AbstractReadOnlyTest {
( (HibernateProxy) dp ).getHibernateLazyInitializer().isReadOnly();
fail( "should have failed because session was detached" );
}
- catch (TransientObjectException ex) {
+ catch (IllegalStateException ex) {
// expected
assertFalse( ( (HibernateProxy) dp ).getHibernateLazyInitializer().isReadOnlySettingAvailable() );
}
@@ -1729,7 +1729,7 @@ public class ReadOnlyProxyTest extends AbstractReadOnlyTest {
s.isReadOnly( dp );
fail( "should have failed because proxy was detached" );
}
- catch (TransientObjectException ex) {
+ catch (IllegalStateException ex) {
// expected
assertFalse( ( (HibernateProxy) dp ).getHibernateLazyInitializer().isReadOnlySettingAvailable() );
}
@@ -1759,7 +1759,7 @@ public class ReadOnlyProxyTest extends AbstractReadOnlyTest {
( (HibernateProxy) dp ).getHibernateLazyInitializer().isReadOnly();
fail( "should have failed because proxy was detached" );
}
- catch (TransientObjectException ex) {
+ catch (IllegalStateException ex) {
// expected
assertFalse( ( (HibernateProxy) dp ).getHibernateLazyInitializer().isReadOnlySettingAvailable() );
}
@@ -1823,7 +1823,7 @@ public class ReadOnlyProxyTest extends AbstractReadOnlyTest {
( (HibernateProxy) dp ).getHibernateLazyInitializer().setReadOnly( true );
fail( "should have failed because session was detached" );
}
- catch (TransientObjectException ex) {
+ catch (IllegalStateException ex) {
// expected
assertFalse( ( (HibernateProxy) dp ).getHibernateLazyInitializer().isReadOnlySettingAvailable() );
}
@@ -1892,7 +1892,7 @@ public class ReadOnlyProxyTest extends AbstractReadOnlyTest {
s.setReadOnly( dp, true );
fail( "should have failed because proxy was detached" );
}
- catch (TransientObjectException ex) {
+ catch (IllegalStateException ex) {
// expected
assertFalse( ( (HibernateProxy) dp ).getHibernateLazyInitializer().isReadOnlySettingAvailable() );
}
@@ -1922,7 +1922,7 @@ public class ReadOnlyProxyTest extends AbstractReadOnlyTest {
( (HibernateProxy) dp ).getHibernateLazyInitializer().setReadOnly( true );
fail( "should have failed because proxy was detached" );
}
- catch (TransientObjectException ex) {
+ catch (IllegalStateException ex) {
// expected
assertFalse( ( (HibernateProxy) dp ).getHibernateLazyInitializer().isReadOnlySettingAvailable() );
}