Search in sources :

Example 1 with TO_PARENT

use of org.hibernate.type.ForeignKeyDirection.TO_PARENT in project hibernate-reactive by hibernate.

the class DefaultReactiveMergeEventListener method entityIsTransient.

protected CompletionStage<Void> entityIsTransient(MergeEvent event, MergeContext copyCache) {
    LOG.trace("Merging transient instance");
    final Object entity = event.getEntity();
    final EventSource session = event.getSession();
    final String entityName = event.getEntityName();
    final EntityPersister persister = session.getEntityPersister(entityName, entity);
    final Serializable id = persister.hasIdentifierProperty() ? persister.getIdentifier(entity, session) : null;
    final Object copy;
    final Object existingCopy = copyCache.get(entity);
    if (existingCopy != null) {
        persister.setIdentifier(copyCache.get(entity), id, session);
        copy = existingCopy;
    } else {
        copy = session.instantiate(persister, id);
        // before cascade!
        copyCache.put(entity, copy, true);
    }
    // cascadeOnMerge(event, persister, entity, copyCache, Cascades.CASCADE_BEFORE_MERGE);
    return super.cascadeBeforeSave(session, persister, entity, copyCache).thenCompose(v -> copyValues(persister, entity, copy, session, copyCache, FROM_PARENT)).thenCompose(v -> saveTransientEntity(copy, entityName, event.getRequestedId(), session, copyCache)).thenCompose(v -> super.cascadeAfterSave(session, persister, entity, copyCache)).thenCompose(v -> copyValues(persister, entity, copy, session, copyCache, TO_PARENT)).thenAccept(v -> {
        event.setResult(copy);
        if (copy instanceof PersistentAttributeInterceptable) {
            final PersistentAttributeInterceptable interceptable = (PersistentAttributeInterceptable) copy;
            final PersistentAttributeInterceptor interceptor = interceptable.$$_hibernate_getInterceptor();
            if (interceptor == null) {
                persister.getBytecodeEnhancementMetadata().injectInterceptor(copy, id, session);
            }
        }
    });
}
Also used : EntityPersister(org.hibernate.persister.entity.EntityPersister) CascadingAction(org.hibernate.reactive.engine.impl.CascadingAction) AssertionFailure(org.hibernate.AssertionFailure) EntityPersister(org.hibernate.persister.entity.EntityPersister) EventSource(org.hibernate.event.spi.EventSource) Log(org.hibernate.reactive.logging.impl.Log) PersistenceContext(org.hibernate.engine.spi.PersistenceContext) ForeignKeyDirection(org.hibernate.type.ForeignKeyDirection) MergeEvent(org.hibernate.event.spi.MergeEvent) LoggerFactory(org.hibernate.reactive.logging.impl.LoggerFactory) MergeContext(org.hibernate.event.internal.MergeContext) ObjectDeletedException(org.hibernate.ObjectDeletedException) TO_PARENT(org.hibernate.type.ForeignKeyDirection.TO_PARENT) HibernateProxy(org.hibernate.proxy.HibernateProxy) FROM_PARENT(org.hibernate.type.ForeignKeyDirection.FROM_PARENT) PersistentAttributeInterceptor(org.hibernate.engine.spi.PersistentAttributeInterceptor) Map(java.util.Map) EventUtil(org.hibernate.event.internal.EventUtil) Cascade(org.hibernate.reactive.engine.impl.Cascade) CompletionStages.voidFuture(org.hibernate.reactive.util.impl.CompletionStages.voidFuture) SessionFactoryImplementor(org.hibernate.engine.spi.SessionFactoryImplementor) ReactiveSession(org.hibernate.reactive.session.ReactiveSession) LazyInitializer(org.hibernate.proxy.LazyInitializer) PersistentAttributeInterceptable(org.hibernate.engine.spi.PersistentAttributeInterceptable) EntityTypes(org.hibernate.reactive.engine.impl.EntityTypes) MethodHandles(java.lang.invoke.MethodHandles) StaleObjectStateException(org.hibernate.StaleObjectStateException) StatisticsImplementor(org.hibernate.stat.spi.StatisticsImplementor) TypeHelper(org.hibernate.type.TypeHelper) EntityKey(org.hibernate.engine.spi.EntityKey) EntityCopyObserver(org.hibernate.event.spi.EntityCopyObserver) ServiceRegistry(org.hibernate.service.ServiceRegistry) Serializable(java.io.Serializable) CompletionStage(java.util.concurrent.CompletionStage) EntityCopyObserverFactory(org.hibernate.event.spi.EntityCopyObserverFactory) ReactiveMergeEventListener(org.hibernate.reactive.event.ReactiveMergeEventListener) MergeEventListener(org.hibernate.event.spi.MergeEventListener) EntityEntry(org.hibernate.engine.spi.EntityEntry) CompletionStages.loop(org.hibernate.reactive.util.impl.CompletionStages.loop) WrongClassException(org.hibernate.WrongClassException) CascadePoint(org.hibernate.engine.internal.CascadePoint) EntityState(org.hibernate.event.internal.EntityState) EnhancementAsProxyLazinessInterceptor(org.hibernate.bytecode.enhance.spi.interceptor.EnhancementAsProxyLazinessInterceptor) SessionImplementor(org.hibernate.engine.spi.SessionImplementor) HibernateException(org.hibernate.HibernateException) Hibernate(org.hibernate.Hibernate) SelfDirtinessTracker(org.hibernate.engine.spi.SelfDirtinessTracker) DETACHED(org.hibernate.event.internal.EntityState.DETACHED) EventSource(org.hibernate.event.spi.EventSource) Serializable(java.io.Serializable) PersistentAttributeInterceptor(org.hibernate.engine.spi.PersistentAttributeInterceptor) PersistentAttributeInterceptable(org.hibernate.engine.spi.PersistentAttributeInterceptable)

Aggregations

Serializable (java.io.Serializable)1 MethodHandles (java.lang.invoke.MethodHandles)1 Map (java.util.Map)1 CompletionStage (java.util.concurrent.CompletionStage)1 AssertionFailure (org.hibernate.AssertionFailure)1 Hibernate (org.hibernate.Hibernate)1 HibernateException (org.hibernate.HibernateException)1 ObjectDeletedException (org.hibernate.ObjectDeletedException)1 StaleObjectStateException (org.hibernate.StaleObjectStateException)1 WrongClassException (org.hibernate.WrongClassException)1 EnhancementAsProxyLazinessInterceptor (org.hibernate.bytecode.enhance.spi.interceptor.EnhancementAsProxyLazinessInterceptor)1 CascadePoint (org.hibernate.engine.internal.CascadePoint)1 EntityEntry (org.hibernate.engine.spi.EntityEntry)1 EntityKey (org.hibernate.engine.spi.EntityKey)1 PersistenceContext (org.hibernate.engine.spi.PersistenceContext)1 PersistentAttributeInterceptable (org.hibernate.engine.spi.PersistentAttributeInterceptable)1 PersistentAttributeInterceptor (org.hibernate.engine.spi.PersistentAttributeInterceptor)1 SelfDirtinessTracker (org.hibernate.engine.spi.SelfDirtinessTracker)1 SessionFactoryImplementor (org.hibernate.engine.spi.SessionFactoryImplementor)1 SessionImplementor (org.hibernate.engine.spi.SessionImplementor)1