org.hibernate.PersistentObjectException Java Examples
The following examples show how to use
org.hibernate.PersistentObjectException.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: DefaultReactiveLoadEventListener.java From hibernate-reactive with GNU Lesser General Public License v2.1 | 6 votes |
/** * Performs the load of an entity. * * @param event The initiating load request event * @param persister The persister corresponding to the entity to be loaded * @param keyToLoad The key of the entity to be loaded * @param options The defined load options * * @return The loaded entity. */ private CompletionStage<Object> load( LoadEvent event, EntityPersister persister, EntityKey keyToLoad, LoadType options) { final EventSource session = event.getSession(); if ( event.getInstanceToLoad() != null ) { if ( session.getPersistenceContextInternal().getEntry( event.getInstanceToLoad() ) != null ) { throw new PersistentObjectException( "attempted to load into an instance that was already associated with the session: " + MessageHelper.infoString( persister, event.getEntityId(), session.getFactory() ) ); } persister.setIdentifier( event.getInstanceToLoad(), event.getEntityId(), session ); } return doLoad( event, persister, keyToLoad, options ) .thenApply( optional -> { boolean isOptionalInstance = event.getInstanceToLoad() != null; if ( optional==null && ( !options.isAllowNulls() || isOptionalInstance ) ) { throwEntityNotFound( session, event.getEntityClassName(), event.getEntityId() ); } else if ( isOptionalInstance && optional != event.getInstanceToLoad() ) { throw new NonUniqueObjectException( event.getEntityId(), event.getEntityClassName() ); } return optional; } ); }
Example #2
Source File: StatefulPersistenceContext.java From lams with GNU General Public License v2.0 | 6 votes |
@Override public Object unproxy(Object maybeProxy) throws HibernateException { if ( maybeProxy instanceof HibernateProxy ) { final HibernateProxy proxy = (HibernateProxy) maybeProxy; final LazyInitializer li = proxy.getHibernateLazyInitializer(); if ( li.isUninitialized() ) { throw new PersistentObjectException( "object was an uninitialized proxy for " + li.getEntityName() ); } //unwrap the object and return return li.getImplementation(); } else { return maybeProxy; } }
Example #3
Source File: StatefulPersistenceContext.java From cacheonix-core with GNU Lesser General Public License v2.1 | 6 votes |
/** * Get the entity instance underlying the given proxy, throwing * an exception if the proxy is uninitialized. If the given object * is not a proxy, simply return the argument. */ public Object unproxy(Object maybeProxy) throws HibernateException { if ( maybeProxy instanceof ElementWrapper ) { maybeProxy = ( (ElementWrapper) maybeProxy ).getElement(); } if ( maybeProxy instanceof HibernateProxy ) { HibernateProxy proxy = (HibernateProxy) maybeProxy; LazyInitializer li = proxy.getHibernateLazyInitializer(); if ( li.isUninitialized() ) { throw new PersistentObjectException( "object was an uninitialized proxy for " + li.getEntityName() ); } return li.getImplementation(); //unwrap the object } else { return maybeProxy; } }
Example #4
Source File: DefaultSaveEventListener.java From lams with GNU General Public License v2.0 | 5 votes |
protected boolean reassociateIfUninitializedProxy(Object object, SessionImplementor source) { if ( !Hibernate.isInitialized(object) ) { throw new PersistentObjectException("uninitialized proxy passed to save()"); } else { return false; } }
Example #5
Source File: DefaultLoadEventListener.java From lams with GNU General Public License v2.0 | 5 votes |
/** * Performs the load of an entity. * * @param event The initiating load request event * @param persister The persister corresponding to the entity to be loaded * @param keyToLoad The key of the entity to be loaded * @param options The defined load options * * @return The loaded entity. * * @throws HibernateException */ private Object load( final LoadEvent event, final EntityPersister persister, final EntityKey keyToLoad, final LoadEventListener.LoadType options) { if ( event.getInstanceToLoad() != null ) { if ( event.getSession().getPersistenceContext().getEntry( event.getInstanceToLoad() ) != null ) { throw new PersistentObjectException( "attempted to load into an instance that was already associated with the session: " + MessageHelper.infoString( persister, event.getEntityId(), event.getSession().getFactory() ) ); } persister.setIdentifier( event.getInstanceToLoad(), event.getEntityId(), event.getSession() ); } final Object entity = doLoad( event, persister, keyToLoad, options ); boolean isOptionalInstance = event.getInstanceToLoad() != null; if ( entity == null && ( !options.isAllowNulls() || isOptionalInstance ) ) { event.getSession() .getFactory() .getEntityNotFoundDelegate() .handleEntityNotFound( event.getEntityClassName(), event.getEntityId() ); } else if ( isOptionalInstance && entity != event.getInstanceToLoad() ) { throw new NonUniqueObjectException( event.getEntityId(), event.getEntityClassName() ); } return entity; }
Example #6
Source File: DefaultSaveEventListener.java From cacheonix-core with GNU Lesser General Public License v2.1 | 5 votes |
protected boolean reassociateIfUninitializedProxy(Object object, SessionImplementor source) { if ( !Hibernate.isInitialized(object) ) { throw new PersistentObjectException("uninitialized proxy passed to save()"); } else { return false; } }
Example #7
Source File: DefaultLoadEventListener.java From cacheonix-core with GNU Lesser General Public License v2.1 | 5 votes |
/** * Perfoms the load of an entity. * * @return The loaded entity. * @throws HibernateException */ protected Object load( final LoadEvent event, final EntityPersister persister, final EntityKey keyToLoad, final LoadEventListener.LoadType options) throws HibernateException { if ( event.getInstanceToLoad() != null ) { if ( event.getSession().getPersistenceContext().getEntry( event.getInstanceToLoad() ) != null ) { throw new PersistentObjectException( "attempted to load into an instance that was already associated with the session: " + MessageHelper.infoString( persister, event.getEntityId(), event.getSession().getFactory() ) ); } persister.setIdentifier( event.getInstanceToLoad(), event.getEntityId(), event.getSession().getEntityMode() ); } Object entity = doLoad(event, persister, keyToLoad, options); boolean isOptionalInstance = event.getInstanceToLoad() != null; if ( !options.isAllowNulls() || isOptionalInstance ) { if ( entity == null ) { event.getSession().getFactory().getEntityNotFoundDelegate().handleEntityNotFound( event.getEntityClassName(), event.getEntityId() ); } } if ( isOptionalInstance && entity != event.getInstanceToLoad() ) { throw new NonUniqueObjectException( event.getEntityId(), event.getEntityClassName() ); } return entity; }
Example #8
Source File: DefaultReactivePersistEventListener.java From hibernate-reactive with GNU Lesser General Public License v2.1 | 4 votes |
/** * Handle the given create event. * * @param event The create event to be handled. */ public CompletionStage<Void> reactiveOnPersist(PersistEvent event, IdentitySet createCache) throws HibernateException { final SessionImplementor source = event.getSession(); final Object object = event.getObject(); final Object entity; if ( object instanceof HibernateProxy ) { LazyInitializer li = ( (HibernateProxy) object ).getHibernateLazyInitializer(); if ( li.isUninitialized() ) { if ( li.getSession() == source ) { return CompletionStages.nullFuture(); //NOTE EARLY EXIT! } else { return CompletionStages.failedFuture( new PersistentObjectException( "uninitialized proxy passed to persist()" ) ); } } entity = li.getImplementation(); } else { entity = object; } final String entityName; if ( event.getEntityName() != null ) { entityName = event.getEntityName(); } else { entityName = source.bestGuessEntityName( entity ); event.setEntityName( entityName ); } final EntityEntry entityEntry = source.getPersistenceContextInternal().getEntry( entity ); EntityState entityState = EntityState.getEntityState( entity, entityName, entityEntry, source, true ); if ( entityState == EntityState.DETACHED ) { // JPA 2, in its version of a "foreign generated", allows the id attribute value // to be manually set by the user, even though this manual value is irrelevant. // The issue is that this causes problems with the Hibernate unsaved-value strategy // which comes into play here in determining detached/transient state. // // Detect if we have this situation and if so null out the id value and calculate the // entity state again. // NOTE: entityEntry must be null to get here, so we cannot use any of its values EntityPersister persister = source.getFactory().getMetamodel().entityPersister( entityName ); if (persister.getIdentifierGenerator() instanceof ForeignGenerator) { if ( LOG.isDebugEnabled() && persister.getIdentifier( entity, source ) != null ) { LOG.debug( "Resetting entity id attribute to null for foreign generator" ); } persister.setIdentifier( entity, null, source ); entityState = EntityState.getEntityState( entity, entityName, entityEntry, source, true ); } } switch ( entityState ) { case DETACHED: { return CompletionStages.failedFuture( new PersistentObjectException( "detached entity passed to persist: " + EventUtil.getLoggableName( event.getEntityName(), entity ) ) ); } case PERSISTENT: { return entityIsPersistent( event, createCache ); } case TRANSIENT: { return entityIsTransient( event, createCache ); } case DELETED: { entityEntry.setStatus( Status.MANAGED ); entityEntry.setDeletedState( null ); event.getSession().getActionQueue().unScheduleDeletion( entityEntry, event.getObject() ); return entityIsDeleted( event, createCache ); } default: { return CompletionStages.failedFuture( new ObjectDeletedException( "deleted entity passed to persist", null, EventUtil.getLoggableName( event.getEntityName(), entity ) ) ); } } }
Example #9
Source File: DefaultPersistEventListener.java From lams with GNU General Public License v2.0 | 4 votes |
/** * Handle the given create event. * * @param event The create event to be handled. * */ public void onPersist(PersistEvent event, Map createCache) throws HibernateException { final SessionImplementor source = event.getSession(); final Object object = event.getObject(); final Object entity; if ( object instanceof HibernateProxy ) { LazyInitializer li = ( (HibernateProxy) object ).getHibernateLazyInitializer(); if ( li.isUninitialized() ) { if ( li.getSession() == source ) { return; //NOTE EARLY EXIT! } else { throw new PersistentObjectException( "uninitialized proxy passed to persist()" ); } } entity = li.getImplementation(); } else { entity = object; } final String entityName; if ( event.getEntityName() != null ) { entityName = event.getEntityName(); } else { entityName = source.bestGuessEntityName( entity ); event.setEntityName( entityName ); } final EntityEntry entityEntry = source.getPersistenceContext().getEntry( entity ); EntityState entityState = getEntityState( entity, entityName, entityEntry, source ); if ( entityState == EntityState.DETACHED ) { // JPA 2, in its version of a "foreign generated", allows the id attribute value // to be manually set by the user, even though this manual value is irrelevant. // The issue is that this causes problems with the Hibernate unsaved-value strategy // which comes into play here in determining detached/transient state. // // Detect if we have this situation and if so null out the id value and calculate the // entity state again. // NOTE: entityEntry must be null to get here, so we cannot use any of its values EntityPersister persister = source.getFactory().getEntityPersister( entityName ); if ( ForeignGenerator.class.isInstance( persister.getIdentifierGenerator() ) ) { if ( LOG.isDebugEnabled() && persister.getIdentifier( entity, source ) != null ) { LOG.debug( "Resetting entity id attribute to null for foreign generator" ); } persister.setIdentifier( entity, null, source ); entityState = getEntityState( entity, entityName, entityEntry, source ); } } switch ( entityState ) { case DETACHED: { throw new PersistentObjectException( "detached entity passed to persist: " + getLoggableName( event.getEntityName(), entity ) ); } case PERSISTENT: { entityIsPersistent( event, createCache ); break; } case TRANSIENT: { entityIsTransient( event, createCache ); break; } case DELETED: { entityEntry.setStatus( Status.MANAGED ); entityEntry.setDeletedState( null ); event.getSession().getActionQueue().unScheduleDeletion( entityEntry, event.getObject() ); entityIsDeleted( event, createCache ); break; } default: { throw new ObjectDeletedException( "deleted entity passed to persist", null, getLoggableName( event.getEntityName(), entity ) ); } } }
Example #10
Source File: DefaultSaveOrUpdateEventListener.java From lams with GNU General Public License v2.0 | 4 votes |
protected Serializable entityIsPersistent(SaveOrUpdateEvent event) throws HibernateException { final boolean traceEnabled = LOG.isTraceEnabled(); if ( traceEnabled ) { LOG.trace( "Ignoring persistent instance" ); } EntityEntry entityEntry = event.getEntry(); if ( entityEntry == null ) { throw new AssertionFailure( "entity was transient or detached" ); } else { if ( entityEntry.getStatus() == Status.DELETED ) { throw new AssertionFailure( "entity was deleted" ); } final SessionFactoryImplementor factory = event.getSession().getFactory(); Serializable requestedId = event.getRequestedId(); Serializable savedId; if ( requestedId == null ) { savedId = entityEntry.getId(); } else { final boolean isEqual = !entityEntry.getPersister().getIdentifierType() .isEqual( requestedId, entityEntry.getId(), factory ); if ( isEqual ) { throw new PersistentObjectException( "object passed to save() was already persistent: " + MessageHelper.infoString( entityEntry.getPersister(), requestedId, factory ) ); } savedId = requestedId; } if ( traceEnabled ) { LOG.tracev( "Object already associated with session: {0}", MessageHelper.infoString( entityEntry.getPersister(), savedId, factory ) ); } return savedId; } }
Example #11
Source File: DefaultPersistEventListener.java From cacheonix-core with GNU Lesser General Public License v2.1 | 4 votes |
/** * Handle the given create event. * * @param event The create event to be handled. * @throws HibernateException */ public void onPersist(PersistEvent event, Map createCache) throws HibernateException { final SessionImplementor source = event.getSession(); final Object object = event.getObject(); final Object entity; if (object instanceof HibernateProxy) { LazyInitializer li = ( (HibernateProxy) object ).getHibernateLazyInitializer(); if ( li.isUninitialized() ) { if ( li.getSession()==source ) { return; //NOTE EARLY EXIT! } else { throw new PersistentObjectException("uninitialized proxy passed to persist()"); } } entity = li.getImplementation(); } else { entity = object; } int entityState = getEntityState( entity, event.getEntityName(), source.getPersistenceContext().getEntry(entity), source ); switch (entityState) { case DETACHED: throw new PersistentObjectException( "detached entity passed to persist: " + getLoggableName( event.getEntityName(), entity ) ); case PERSISTENT: entityIsPersistent(event, createCache); break; case TRANSIENT: entityIsTransient(event, createCache); break; default: throw new ObjectDeletedException( "deleted entity passed to persist", null, getLoggableName( event.getEntityName(), entity ) ); } }
Example #12
Source File: DefaultSaveOrUpdateEventListener.java From cacheonix-core with GNU Lesser General Public License v2.1 | 4 votes |
protected Serializable entityIsPersistent(SaveOrUpdateEvent event) throws HibernateException { log.trace( "ignoring persistent instance" ); EntityEntry entityEntry = event.getEntry(); if ( entityEntry == null ) { throw new AssertionFailure( "entity was transient or detached" ); } else { if ( entityEntry.getStatus() == Status.DELETED ) { throw new AssertionFailure( "entity was deleted" ); } final SessionFactoryImplementor factory = event.getSession().getFactory(); Serializable requestedId = event.getRequestedId(); Serializable savedId; if ( requestedId == null ) { savedId = entityEntry.getId(); } else { final boolean isEqual = !entityEntry.getPersister().getIdentifierType() .isEqual( requestedId, entityEntry.getId(), event.getSession().getEntityMode(), factory ); if ( isEqual ) { throw new PersistentObjectException( "object passed to save() was already persistent: " + MessageHelper.infoString( entityEntry.getPersister(), requestedId, factory ) ); } savedId = requestedId; } if ( log.isTraceEnabled() ) { log.trace( "object already associated with session: " + MessageHelper.infoString( entityEntry.getPersister(), savedId, factory ) ); } return savedId; } }