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);
}
}
});
}
Aggregations