Java Code Examples for org.hibernate.engine.spi.EntityEntry#getStatus()
The following examples show how to use
org.hibernate.engine.spi.EntityEntry#getStatus() .
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: AbstractReactiveFlushingEventListener.java From hibernate-reactive with GNU Lesser General Public License v2.1 | 6 votes |
/** * process cascade save/update at the start of a flush to discover * any newly referenced entity that must be passed to saveOrUpdate(), * and also apply orphan delete */ private CompletionStage<Void> prepareEntityFlushes(EventSource session, PersistenceContext persistenceContext) throws HibernateException { LOG.debug( "Processing flush-time cascades" ); CompletionStage<Void> stage = CompletionStages.nullFuture(); final IdentitySet copiedAlready = new IdentitySet( 10 ); //safe from concurrent modification because of how concurrentEntries() is implemented on IdentityMap for ( Map.Entry<Object, EntityEntry> me : persistenceContext.reentrantSafeEntityEntries() ) { EntityEntry entry = me.getValue(); Status status = entry.getStatus(); if ( status == Status.MANAGED || status == Status.SAVING || status == Status.READ_ONLY ) { stage = stage.thenCompose( v -> cascadeOnFlush( session, entry.getPersister(), me.getKey(), copiedAlready ) ); } } return stage; }
Example 2
Source File: DefaultSaveOrUpdateEventListener.java From lams with GNU General Public License v2.0 | 6 votes |
/** * The given save-update event named a transient entity. * <p/> * Here, we will perform the save processing. * * @param event The save event to be handled. * * @return The entity's identifier after saving. */ protected Serializable entityIsTransient(SaveOrUpdateEvent event) { LOG.trace( "Saving transient instance" ); final EventSource source = event.getSession(); EntityEntry entityEntry = event.getEntry(); if ( entityEntry != null ) { if ( entityEntry.getStatus() == Status.DELETED ) { source.forceFlush( entityEntry ); } else { throw new AssertionFailure( "entity was persistent" ); } } Serializable id = saveWithGeneratedOrRequestedId( event ); source.getPersistenceContext().reassociateProxy( event.getObject(), id ); return id; }
Example 3
Source File: DefaultFlushEntityEventListener.java From lams with GNU General Public License v2.0 | 6 votes |
protected boolean invokeInterceptor( SessionImplementor session, Object entity, EntityEntry entry, final Object[] values, EntityPersister persister) { boolean isDirty = false; if ( entry.getStatus() != Status.DELETED ) { if ( callbackRegistry.preUpdate( entity ) ) { isDirty = copyState( entity, persister.getPropertyTypes(), values, session.getFactory() ); } } final boolean answerFromInterceptor = session.getInterceptor().onFlushDirty( entity, entry.getId(), values, entry.getLoadedState(), persister.getPropertyNames(), persister.getPropertyTypes() ); return answerFromInterceptor || isDirty; }
Example 4
Source File: GrailsHibernateUtil.java From gorm-hibernate5 with Apache License 2.0 | 6 votes |
/** * Sets the target object to read-write, allowing Hibernate to dirty check it and auto-flush changes. * * @see #setObjectToReadyOnly(Object, org.hibernate.SessionFactory) * * @param target The target object * @param sessionFactory The SessionFactory instance */ public static void setObjectToReadWrite(final Object target, SessionFactory sessionFactory) { Session session = sessionFactory.getCurrentSession(); if (!canModifyReadWriteState(session, target)) { return; } SessionImplementor sessionImpl = (SessionImplementor) session; EntityEntry ee = sessionImpl.getPersistenceContext().getEntry(target); if (ee == null || ee.getStatus() != Status.READ_ONLY) { return; } Object actualTarget = target; if (target instanceof HibernateProxy) { actualTarget = ((HibernateProxy)target).getHibernateLazyInitializer().getImplementation(); } session.setReadOnly(actualTarget, false); session.setHibernateFlushMode(FlushMode.AUTO); incrementVersion(target); }
Example 5
Source File: DefaultUpdateEventListener.java From lams with GNU General Public License v2.0 | 6 votes |
protected Serializable performSaveOrUpdate(SaveOrUpdateEvent event) { // this implementation is supposed to tolerate incorrect unsaved-value // mappings, for the purpose of backward-compatibility EntityEntry entry = event.getSession().getPersistenceContext().getEntry( event.getEntity() ); if ( entry!=null ) { if ( entry.getStatus()== Status.DELETED ) { throw new ObjectDeletedException( "deleted instance passed to update()", null, event.getEntityName() ); } else { return entityIsPersistent(event); } } else { entityIsDetached(event); return null; } }
Example 6
Source File: AbstractFlushingEventListener.java From lams with GNU General Public License v2.0 | 6 votes |
/** * process cascade save/update at the start of a flush to discover * any newly referenced entity that must be passed to saveOrUpdate(), * and also apply orphan delete */ private void prepareEntityFlushes(EventSource session, PersistenceContext persistenceContext) throws HibernateException { LOG.debug( "Processing flush-time cascades" ); final Object anything = getAnything(); //safe from concurrent modification because of how concurrentEntries() is implemented on IdentityMap for ( Map.Entry<Object,EntityEntry> me : persistenceContext.reentrantSafeEntityEntries() ) { // for ( Map.Entry me : IdentityMap.concurrentEntries( persistenceContext.getEntityEntries() ) ) { EntityEntry entry = (EntityEntry) me.getValue(); Status status = entry.getStatus(); if ( status == Status.MANAGED || status == Status.SAVING || status == Status.READ_ONLY ) { cascadeOnFlush( session, entry.getPersister(), me.getKey(), anything ); } } }
Example 7
Source File: DefaultFlushEntityEventListener.java From lams with GNU General Public License v2.0 | 6 votes |
private Object[] getValues(Object entity, EntityEntry entry, boolean mightBeDirty, SessionImplementor session) { final Object[] loadedState = entry.getLoadedState(); final Status status = entry.getStatus(); final EntityPersister persister = entry.getPersister(); final Object[] values; if ( status == Status.DELETED ) { //grab its state saved at deletion values = entry.getDeletedState(); } else if ( !mightBeDirty && loadedState != null ) { values = loadedState; } else { checkId( entity, persister, entry.getId(), session ); // grab its current state values = persister.getPropertyValues( entity ); checkNaturalId( persister, entry, values, loadedState, session ); } return values; }
Example 8
Source File: PostUpdateEventListenerStandardImpl.java From lams with GNU General Public License v2.0 | 5 votes |
private void handlePostUpdate(Object entity, EventSource source) { EntityEntry entry = source.getPersistenceContext().getEntry( entity ); // mimic the preUpdate filter if ( Status.DELETED != entry.getStatus()) { callbackRegistry.postUpdate(entity); } }
Example 9
Source File: AbstractSaveEventListener.java From lams with GNU General Public License v2.0 | 5 votes |
/** * Determine whether the entity is persistent, detached, or transient * * @param entity The entity to check * @param entityName The name of the entity * @param entry The entity's entry in the persistence context * @param source The originating session. * * @return The state. */ protected EntityState getEntityState( Object entity, String entityName, EntityEntry entry, //pass this as an argument only to avoid double looking SessionImplementor source) { final boolean traceEnabled = LOG.isTraceEnabled(); if ( entry != null ) { // the object is persistent //the entity is associated with the session, so check its status if ( entry.getStatus() != Status.DELETED ) { // do nothing for persistent instances if ( traceEnabled ) { LOG.tracev( "Persistent instance of: {0}", getLoggableName( entityName, entity ) ); } return EntityState.PERSISTENT; } // ie. e.status==DELETED if ( traceEnabled ) { LOG.tracev( "Deleted instance of: {0}", getLoggableName( entityName, entity ) ); } return EntityState.DELETED; } // the object is transient or detached // the entity is not associated with the session, so // try interceptor and unsaved-value if ( ForeignKeys.isTransient( entityName, entity, getAssumedUnsaved(), source ) ) { if ( traceEnabled ) { LOG.tracev( "Transient instance of: {0}", getLoggableName( entityName, entity ) ); } return EntityState.TRANSIENT; } if ( traceEnabled ) { LOG.tracev( "Detached instance of: {0}", getLoggableName( entityName, entity ) ); } return EntityState.DETACHED; }
Example 10
Source File: AbstractFlushingEventListener.java From lams with GNU General Public License v2.0 | 5 votes |
/** * 1. detect any dirty entities * 2. schedule any entity updates * 3. search out any reachable collections */ private int flushEntities(final FlushEvent event, final PersistenceContext persistenceContext) throws HibernateException { LOG.trace( "Flushing entities and processing referenced collections" ); final EventSource source = event.getSession(); final Iterable<FlushEntityEventListener> flushListeners = source.getFactory().getServiceRegistry() .getService( EventListenerRegistry.class ) .getEventListenerGroup( EventType.FLUSH_ENTITY ) .listeners(); // Among other things, updateReachables() will recursively load all // collections that are moving roles. This might cause entities to // be loaded. // So this needs to be safe from concurrent modification problems. final Map.Entry<Object,EntityEntry>[] entityEntries = persistenceContext.reentrantSafeEntityEntries(); final int count = entityEntries.length; for ( Map.Entry<Object,EntityEntry> me : entityEntries ) { // Update the status of the object and if necessary, schedule an update EntityEntry entry = me.getValue(); Status status = entry.getStatus(); if ( status != Status.LOADING && status != Status.GONE ) { final FlushEntityEvent entityEvent = new FlushEntityEvent( source, me.getKey(), entry ); for ( FlushEntityEventListener listener : flushListeners ) { listener.onFlushEntity( entityEvent ); } } } source.getActionQueue().sortActions(); return count; }
Example 11
Source File: DefaultFlushEntityEventListener.java From lams with GNU General Public License v2.0 | 5 votes |
/** * Flushes a single entity's state to the database, by scheduling * an update action, if necessary */ public void onFlushEntity(FlushEntityEvent event) throws HibernateException { final Object entity = event.getEntity(); final EntityEntry entry = event.getEntityEntry(); final EventSource session = event.getSession(); final EntityPersister persister = entry.getPersister(); final Status status = entry.getStatus(); final Type[] types = persister.getPropertyTypes(); final boolean mightBeDirty = entry.requiresDirtyCheck( entity ); final Object[] values = getValues( entity, entry, mightBeDirty, session ); event.setPropertyValues( values ); //TODO: avoid this for non-new instances where mightBeDirty==false boolean substitute = wrapCollections( session, persister, types, values ); if ( isUpdateNecessary( event, mightBeDirty ) ) { substitute = scheduleUpdate( event ) || substitute; } if ( status != Status.DELETED ) { // now update the object .. has to be outside the main if block above (because of collections) if ( substitute ) { persister.setPropertyValues( entity, values ); } // Search for collections by reachability, updating their role. // We don't want to touch collections reachable from a deleted object if ( persister.hasCollections() ) { new FlushVisitor( session, entity ).processEntityPropertyValues( values, types ); } } }
Example 12
Source File: AbstractLockUpgradeEventListener.java From lams with GNU General Public License v2.0 | 4 votes |
/** * Performs a pessimistic lock upgrade on a given entity, if needed. * * @param object The entity for which to upgrade the lock. * @param entry The entity's EntityEntry instance. * @param lockOptions contains the requested lock mode. * @param source The session which is the source of the event being processed. */ protected void upgradeLock(Object object, EntityEntry entry, LockOptions lockOptions, EventSource source) { LockMode requestedLockMode = lockOptions.getLockMode(); if ( requestedLockMode.greaterThan( entry.getLockMode() ) ) { // The user requested a "greater" (i.e. more restrictive) form of // pessimistic lock if ( entry.getStatus() != Status.MANAGED ) { throw new ObjectDeletedException( "attempted to lock a deleted instance", entry.getId(), entry.getPersister().getEntityName() ); } final EntityPersister persister = entry.getPersister(); if ( log.isTraceEnabled() ) { log.tracev( "Locking {0} in mode: {1}", MessageHelper.infoString( persister, entry.getId(), source.getFactory() ), requestedLockMode ); } final boolean cachingEnabled = persister.canWriteToCache(); SoftLock lock = null; Object ck = null; try { if ( cachingEnabled ) { EntityDataAccess cache = persister.getCacheAccessStrategy(); ck = cache.generateCacheKey( entry.getId(), persister, source.getFactory(), source.getTenantIdentifier() ); lock = cache.lockItem( source, ck, entry.getVersion() ); } if ( persister.isVersioned() && requestedLockMode == LockMode.FORCE ) { // todo : should we check the current isolation mode explicitly? Object nextVersion = persister.forceVersionIncrement( entry.getId(), entry.getVersion(), source ); entry.forceLocked( object, nextVersion ); } else { persister.lock( entry.getId(), entry.getVersion(), object, lockOptions, source ); } entry.setLockMode(requestedLockMode); } finally { // the database now holds a lock + the object is flushed from the cache, // so release the soft lock if ( cachingEnabled ) { persister.getCacheAccessStrategy().unlockItem( source, ck, lock ); } } } }
Example 13
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 14
Source File: DefaultFlushEntityEventListener.java From lams with GNU General Public License v2.0 | 4 votes |
private void checkNaturalId( EntityPersister persister, EntityEntry entry, Object[] current, Object[] loaded, SessionImplementor session) { if ( persister.hasNaturalIdentifier() && entry.getStatus() != Status.READ_ONLY ) { if ( !persister.getEntityMetamodel().hasImmutableNaturalId() ) { // SHORT-CUT: if the natural id is mutable (!immutable), no need to do the below checks // EARLY EXIT!!! return; } final int[] naturalIdentifierPropertiesIndexes = persister.getNaturalIdentifierProperties(); final Type[] propertyTypes = persister.getPropertyTypes(); final boolean[] propertyUpdateability = persister.getPropertyUpdateability(); final Object[] snapshot = loaded == null ? session.getPersistenceContext().getNaturalIdSnapshot( entry.getId(), persister ) : session.getPersistenceContext().getNaturalIdHelper().extractNaturalIdValues( loaded, persister ); for ( int i = 0; i < naturalIdentifierPropertiesIndexes.length; i++ ) { final int naturalIdentifierPropertyIndex = naturalIdentifierPropertiesIndexes[i]; if ( propertyUpdateability[naturalIdentifierPropertyIndex] ) { // if the given natural id property is updatable (mutable), there is nothing to check continue; } final Type propertyType = propertyTypes[naturalIdentifierPropertyIndex]; if ( !propertyType.isEqual( current[naturalIdentifierPropertyIndex], snapshot[i] ) ) { throw new HibernateException( String.format( "An immutable natural identifier of entity %s was altered from %s to %s", persister.getEntityName(), propertyTypes[naturalIdentifierPropertyIndex].toLoggableString( snapshot[i], session.getFactory() ), propertyTypes[naturalIdentifierPropertyIndex].toLoggableString( current[naturalIdentifierPropertyIndex], session.getFactory() ) ) ); } } } }
Example 15
Source File: DefaultDeleteEventListener.java From lams with GNU General Public License v2.0 | 4 votes |
/** * Handle the given delete event. This is the cascaded form. * * @param event The delete event. * @param transientEntities The cache of entities already deleted * * @throws HibernateException */ public void onDelete(DeleteEvent event, Set transientEntities) throws HibernateException { final EventSource source = event.getSession(); final PersistenceContext persistenceContext = source.getPersistenceContext(); Object entity = persistenceContext.unproxyAndReassociate( event.getObject() ); EntityEntry entityEntry = persistenceContext.getEntry( entity ); final EntityPersister persister; final Serializable id; final Object version; if ( entityEntry == null ) { LOG.trace( "Entity was not persistent in delete processing" ); persister = source.getEntityPersister( event.getEntityName(), entity ); if ( ForeignKeys.isTransient( persister.getEntityName(), entity, null, source ) ) { deleteTransientEntity( source, entity, event.isCascadeDeleteEnabled(), persister, transientEntities ); // EARLY EXIT!!! return; } performDetachedEntityDeletionCheck( event ); id = persister.getIdentifier( entity, source ); if ( id == null ) { throw new TransientObjectException( "the detached instance passed to delete() had a null identifier" ); } final EntityKey key = source.generateEntityKey( id, persister ); persistenceContext.checkUniqueness( key, entity ); new OnUpdateVisitor( source, id, entity ).process( entity, persister ); version = persister.getVersion( entity ); entityEntry = persistenceContext.addEntity( entity, (persister.isMutable() ? Status.MANAGED : Status.READ_ONLY), persister.getPropertyValues( entity ), key, version, LockMode.NONE, true, persister, false ); } else { LOG.trace( "Deleting a persistent instance" ); if ( entityEntry.getStatus() == Status.DELETED || entityEntry.getStatus() == Status.GONE ) { LOG.trace( "Object was already deleted" ); return; } persister = entityEntry.getPersister(); id = entityEntry.getId(); version = entityEntry.getVersion(); } /*if ( !persister.isMutable() ) { throw new HibernateException( "attempted to delete an object of immutable class: " + MessageHelper.infoString(persister) ); }*/ if ( invokeDeleteLifecycle( source, entity, persister ) ) { return; } deleteEntity( source, entity, entityEntry, event.isCascadeDeleteEnabled(), event.isOrphanRemovalBeforeUpdates(), persister, transientEntities ); if ( source.getFactory().getSettings().isIdentifierRollbackEnabled() ) { persister.resetIdentifier( entity, id, version, source ); } }
Example 16
Source File: SessionImpl.java From lams with GNU General Public License v2.0 | 4 votes |
protected void performAnyNeededCrossReferenceSynchronizations() { if ( !synchronizationEnabled ) { // synchronization (this process) was disabled return; } if ( entityPersister.getEntityMetamodel().hasImmutableNaturalId() ) { // only mutable natural-ids need this processing return; } if ( !isTransactionInProgress() ) { // not in a transaction so skip synchronization return; } final boolean debugEnabled = log.isDebugEnabled(); for ( Serializable pk : getPersistenceContext().getNaturalIdHelper() .getCachedPkResolutions( entityPersister ) ) { final EntityKey entityKey = generateEntityKey( pk, entityPersister ); final Object entity = getPersistenceContext().getEntity( entityKey ); final EntityEntry entry = getPersistenceContext().getEntry( entity ); if ( entry == null ) { if ( debugEnabled ) { log.debug( "Cached natural-id/pk resolution linked to null EntityEntry in persistence context : " + MessageHelper.infoString( entityPersister, pk, getFactory() ) ); } continue; } if ( !entry.requiresDirtyCheck( entity ) ) { continue; } // MANAGED is the only status we care about here... if ( entry.getStatus() != Status.MANAGED ) { continue; } getPersistenceContext().getNaturalIdHelper().handleSynchronization( entityPersister, pk, entity ); } }
Example 17
Source File: UnresolvedEntityInsertActions.java From lams with GNU General Public License v2.0 | 4 votes |
/** * Resolve any dependencies on {@code managedEntity}. * * @param managedEntity - the managed entity name * @param session - the session * * @return the insert actions that depended only on the specified entity. * * @throws IllegalArgumentException if {@code managedEntity} did not have managed or read-only status. */ @SuppressWarnings({ "unchecked" }) public Set<AbstractEntityInsertAction> resolveDependentActions(Object managedEntity, SessionImplementor session) { final EntityEntry entityEntry = session.getPersistenceContext().getEntry( managedEntity ); if ( entityEntry.getStatus() != Status.MANAGED && entityEntry.getStatus() != Status.READ_ONLY ) { throw new IllegalArgumentException( "EntityEntry did not have status MANAGED or READ_ONLY: " + entityEntry ); } final boolean traceEnabled = LOG.isTraceEnabled(); // Find out if there are any unresolved insertions that are waiting for the // specified entity to be resolved. final Set<AbstractEntityInsertAction> dependentActions = dependentActionsByTransientEntity.remove( managedEntity ); if ( dependentActions == null ) { if ( traceEnabled ) { LOG.tracev( "No unresolved entity inserts that depended on [{0}]", MessageHelper.infoString( entityEntry.getEntityName(), entityEntry.getId() ) ); } // NOTE EARLY EXIT! return Collections.emptySet(); } final Set<AbstractEntityInsertAction> resolvedActions = new IdentitySet( ); if ( traceEnabled ) { LOG.tracev( "Unresolved inserts before resolving [{0}]: [{1}]", MessageHelper.infoString( entityEntry.getEntityName(), entityEntry.getId() ), toString() ); } for ( AbstractEntityInsertAction dependentAction : dependentActions ) { if ( traceEnabled ) { LOG.tracev( "Resolving insert [{0}] dependency on [{1}]", MessageHelper.infoString( dependentAction.getEntityName(), dependentAction.getId() ), MessageHelper.infoString( entityEntry.getEntityName(), entityEntry.getId() ) ); } final NonNullableTransientDependencies dependencies = dependenciesByAction.get( dependentAction ); dependencies.resolveNonNullableTransientEntity( managedEntity ); if ( dependencies.isEmpty() ) { if ( traceEnabled ) { LOG.tracev( "Resolving insert [{0}] (only depended on [{1}])", dependentAction, MessageHelper.infoString( entityEntry.getEntityName(), entityEntry.getId() ) ); } // dependentAction only depended on managedEntity.. dependenciesByAction.remove( dependentAction ); resolvedActions.add( dependentAction ); } } if ( traceEnabled ) { LOG.tracev( "Unresolved inserts after resolving [{0}]: [{1}]", MessageHelper.infoString( entityEntry.getEntityName(), entityEntry.getId() ), toString() ); } return resolvedActions; }
Example 18
Source File: Cascade.java From lams with GNU General Public License v2.0 | 4 votes |
private static void cascadeLogicalOneToOneOrphanRemoval( final CascadingAction action, final EventSource eventSource, final int componentPathStackDepth, final Object parent, final Object child, final Type type, final CascadeStyle style, final String propertyName, final boolean isCascadeDeleteEnabled) throws HibernateException { // potentially we need to handle orphan deletes for one-to-ones here... if ( isLogicalOneToOne( type ) ) { // We have a physical or logical one-to-one. See if the attribute cascade settings and action-type require // orphan checking if ( style.hasOrphanDelete() && action.deleteOrphans() ) { // value is orphaned if loaded state for this property shows not null // because it is currently null. final EntityEntry entry = eventSource.getPersistenceContext().getEntry( parent ); if ( entry != null && entry.getStatus() != Status.SAVING ) { Object loadedValue; if ( componentPathStackDepth == 0 ) { // association defined on entity loadedValue = entry.getLoadedValue( propertyName ); } else { // association defined on component // todo : this is currently unsupported because of the fact that // we do not know the loaded state of this value properly // and doing so would be very difficult given how components and // entities are loaded (and how 'loaded state' is put into the // EntityEntry). Solutions here are to either: // 1) properly account for components as a 2-phase load construct // 2) just assume the association was just now orphaned and // issue the orphan delete. This would require a special // set of SQL statements though since we do not know the // orphaned value, something a delete with a subquery to // match the owner. // final EntityType entityType = (EntityType) type; // final String getPropertyPath = composePropertyPath( entityType.getPropertyName() ); loadedValue = null; } // orphaned if the association was nulled (child == null) or receives a new value while the // entity is managed (without first nulling and manually flushing). if ( child == null || ( loadedValue != null && child != loadedValue ) ) { EntityEntry valueEntry = eventSource .getPersistenceContext().getEntry( loadedValue ); if ( valueEntry == null && loadedValue instanceof HibernateProxy ) { // un-proxy and re-associate for cascade operation // useful for @OneToOne defined as FetchType.LAZY loadedValue = eventSource.getPersistenceContext().unproxyAndReassociate( loadedValue ); valueEntry = eventSource.getPersistenceContext().getEntry( loadedValue ); // HHH-11965 // Should the unwrapped proxy value be equal via reference to the entity's property value // provided by the 'child' variable, we should not trigger the orphan removal of the // associated one-to-one. if ( child == loadedValue ) { // do nothing return; } } if ( valueEntry != null ) { final String entityName = valueEntry.getPersister().getEntityName(); if ( LOG.isTraceEnabled() ) { final Serializable id = valueEntry.getPersister().getIdentifier( loadedValue, eventSource ); final String description = MessageHelper.infoString( entityName, id ); LOG.tracev( "Deleting orphaned entity instance: {0}", description ); } if ( type.isAssociationType() && ( (AssociationType) type ).getForeignKeyDirection().equals( ForeignKeyDirection.TO_PARENT ) ) { // If FK direction is to-parent, we must remove the orphan *before* the queued update(s) // occur. Otherwise, replacing the association on a managed entity, without manually // nulling and flushing, causes FK constraint violations. eventSource.removeOrphanBeforeUpdates( entityName, loadedValue ); } else { // Else, we must delete after the updates. eventSource.delete( entityName, loadedValue, isCascadeDeleteEnabled, new HashSet() ); } } } } } } }
Example 19
Source File: OptimisticLockingBidirectionalChildUpdatesRootVersionTest.java From high-performance-java-persistence with Apache License 2.0 | 4 votes |
private void incrementRootVersion(FlushEntityEvent event, Object root) { EntityEntry entityEntry = event.getSession().getPersistenceContext().getEntry( Hibernate.unproxy( root) ); if(entityEntry.getStatus() != Status.DELETED) { event.getSession().lock(root, LockMode.OPTIMISTIC_FORCE_INCREMENT); } }
Example 20
Source File: DefaultReactiveLockEventListener.java From hibernate-reactive with GNU Lesser General Public License v2.1 | 4 votes |
/** * Performs a pessimistic lock upgrade on a given entity, if needed. * * @param object The entity for which to upgrade the lock. * @param entry The entity's EntityEntry instance. * @param lockOptions contains the requested lock mode. * @param source The session which is the source of the event being processed. */ protected CompletionStage<Void> upgradeLock(Object object, EntityEntry entry, LockOptions lockOptions, EventSource source) { LockMode requestedLockMode = lockOptions.getLockMode(); if ( requestedLockMode.greaterThan( entry.getLockMode() ) ) { // The user requested a "greater" (i.e. more restrictive) form of // pessimistic lock if ( entry.getStatus() != Status.MANAGED ) { throw new ObjectDeletedException( "attempted to lock a deleted instance", entry.getId(), entry.getPersister().getEntityName() ); } final EntityPersister persister = entry.getPersister(); if ( log.isTraceEnabled() ) { log.tracev( "Locking {0} in mode: {1}", MessageHelper.infoString( persister, entry.getId(), source.getFactory() ), requestedLockMode ); } final boolean cachingEnabled = persister.canWriteToCache(); final SoftLock lock; final Object ck; if ( cachingEnabled ) { EntityDataAccess cache = persister.getCacheAccessStrategy(); ck = cache.generateCacheKey( entry.getId(), persister, source.getFactory(), source.getTenantIdentifier() ); lock = cache.lockItem( source, ck, entry.getVersion() ); } else { lock = null; ck = null; } return ((ReactiveEntityPersister) persister).lockReactive( entry.getId(), entry.getVersion(), object, lockOptions, source ).thenAccept( v -> entry.setLockMode(requestedLockMode) ) .whenComplete( (r, e) -> { // the database now holds a lock + the object is flushed from the cache, // so release the soft lock if ( cachingEnabled ) { persister.getCacheAccessStrategy().unlockItem( source, ck, lock ); } } ); } else { return CompletionStages.nullFuture(); } }