use of org.hibernate.event.spi.EventSource in project hibernate-orm by hibernate.
the class DefaultMergeEventListener method entityIsTransient.
protected void entityIsTransient(MergeEvent event, Map copyCache) {
LOG.trace("Merging transient instance");
final Object entity = event.getEntity();
final EventSource source = event.getSession();
final String entityName = event.getEntityName();
final EntityPersister persister = source.getEntityPersister(entityName, entity);
final Serializable id = persister.hasIdentifierProperty() ? persister.getIdentifier(entity, source) : null;
if (copyCache.containsKey(entity)) {
persister.setIdentifier(copyCache.get(entity), id, source);
} else {
//beforeQuery cascade!
((MergeContext) copyCache).put(entity, source.instantiate(persister, id), true);
}
final Object copy = copyCache.get(entity);
// cascade first, so that all unsaved objects get their
// copy created beforeQuery we actually copy
//cascadeOnMerge(event, persister, entity, copyCache, Cascades.CASCADE_BEFORE_MERGE);
super.cascadeBeforeSave(source, persister, entity, copyCache);
copyValues(persister, entity, copy, source, copyCache, ForeignKeyDirection.FROM_PARENT);
saveTransientEntity(copy, entityName, event.getRequestedId(), source, copyCache);
// cascade first, so that all unsaved objects get their
// copy created beforeQuery we actually copy
super.cascadeAfterSave(source, persister, entity, copyCache);
copyValues(persister, entity, copy, source, copyCache, ForeignKeyDirection.TO_PARENT);
event.setResult(copy);
}
use of org.hibernate.event.spi.EventSource in project hibernate-orm by hibernate.
the class OnReplicateVisitor method processCollection.
@Override
public Object processCollection(Object collection, CollectionType type) throws HibernateException {
if (collection == CollectionType.UNFETCHED_COLLECTION) {
return null;
}
final EventSource session = getSession();
final CollectionPersister persister = session.getFactory().getMetamodel().collectionPersister(type.getRole());
if (isUpdate) {
removeCollection(persister, extractCollectionKeyFromOwner(persister), session);
}
if (collection != null && collection instanceof PersistentCollection) {
final PersistentCollection wrapper = (PersistentCollection) collection;
wrapper.setCurrentSession((SessionImplementor) session);
if (wrapper.wasInitialized()) {
session.getPersistenceContext().addNewCollection(persister, wrapper);
} else {
reattachCollection(wrapper, type);
}
} else {
// otherwise a null or brand new collection
// this will also (inefficiently) handle arrays, which
// have no snapshot, so we can't do any better
//processArrayOrNewCollection(collection, type);
}
return null;
}
use of org.hibernate.event.spi.EventSource in project hibernate-orm by hibernate.
the class OnUpdateVisitor method processCollection.
@Override
Object processCollection(Object collection, CollectionType type) throws HibernateException {
if (collection == CollectionType.UNFETCHED_COLLECTION) {
return null;
}
EventSource session = getSession();
CollectionPersister persister = session.getFactory().getCollectionPersister(type.getRole());
final Serializable collectionKey = extractCollectionKeyFromOwner(persister);
if (collection != null && (collection instanceof PersistentCollection)) {
PersistentCollection wrapper = (PersistentCollection) collection;
if (wrapper.setCurrentSession(session)) {
//a "detached" collection!
if (!isOwnerUnchanged(wrapper, persister, collectionKey)) {
// if the collection belonged to a different entity,
// clean up the existing state of the collection
removeCollection(persister, collectionKey, session);
}
reattachCollection(wrapper, type);
} else {
// a collection loaded in the current session
// can not possibly be the collection belonging
// to the entity passed to update()
removeCollection(persister, collectionKey, session);
}
} else {
// null or brand new collection
// this will also (inefficiently) handle arrays, which have
// no snapshot, so we can't do any better
removeCollection(persister, collectionKey, session);
}
return null;
}
use of org.hibernate.event.spi.EventSource in project hibernate-orm by hibernate.
the class DefaultSaveOrUpdateEventListener method entityIsTransient.
/**
* 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 afterQuery 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;
}
use of org.hibernate.event.spi.EventSource in project hibernate-orm by hibernate.
the class JpaDeleteEventListener method performDetachedEntityDeletionCheck.
@Override
protected void performDetachedEntityDeletionCheck(DeleteEvent event) {
EventSource source = event.getSession();
String entityName = event.getEntityName();
EntityPersister persister = source.getEntityPersister(entityName, event.getObject());
Serializable id = persister.getIdentifier(event.getObject(), source);
entityName = entityName == null ? source.guessEntityName(event.getObject()) : entityName;
throw new IllegalArgumentException("Removing a detached instance " + entityName + "#" + id);
}
Aggregations