use of org.hibernate.engine.spi.EntityKey in project hibernate-orm by hibernate.
the class StatefulPersistenceContext method getDatabaseSnapshot.
/**
* Get the current state of the entity as known to the underlying
* database, or null if there is no corresponding row
* <p/>
* {@inheritDoc}
*/
@Override
public Object[] getDatabaseSnapshot(Serializable id, EntityPersister persister) throws HibernateException {
final EntityKey key = session.generateEntityKey(id, persister);
final Object cached = entitySnapshotsByKey.get(key);
if (cached != null) {
return cached == NO_ROW ? null : (Object[]) cached;
} else {
final Object[] snapshot = persister.getDatabaseSnapshot(id, session);
entitySnapshotsByKey.put(key, snapshot == null ? NO_ROW : snapshot);
return snapshot;
}
}
use of org.hibernate.engine.spi.EntityKey in project hibernate-orm by hibernate.
the class StatefulPersistenceContext method replaceDelayedEntityIdentityInsertKeys.
@Override
public void replaceDelayedEntityIdentityInsertKeys(EntityKey oldKey, Serializable generatedId) {
final Object entity = entitiesByKey.remove(oldKey);
final EntityEntry oldEntry = entityEntryContext.removeEntityEntry(entity);
parentsByChild.clear();
final EntityKey newKey = session.generateEntityKey(generatedId, oldEntry.getPersister());
addEntity(newKey, entity);
addEntry(entity, oldEntry.getStatus(), oldEntry.getLoadedState(), oldEntry.getRowId(), generatedId, oldEntry.getVersion(), oldEntry.getLockMode(), oldEntry.isExistsInDatabase(), oldEntry.getPersister(), oldEntry.isBeingReplicated());
}
use of org.hibernate.engine.spi.EntityKey in project hibernate-orm by hibernate.
the class AbstractReassociateEventListener method reassociate.
/**
* Associates a given entity (either transient or associated with another session) to
* the given session.
*
* @param event The event triggering the re-association
* @param object The entity to be associated
* @param id The id of the entity.
* @param persister The entity's persister instance.
*
* @return An EntityEntry representing the entity within this session.
*/
protected final EntityEntry reassociate(AbstractEvent event, Object object, Serializable id, EntityPersister persister) {
if (log.isTraceEnabled()) {
log.tracev("Reassociating transient instance: {0}", MessageHelper.infoString(persister, id, event.getSession().getFactory()));
}
final EventSource source = event.getSession();
final EntityKey key = source.generateEntityKey(id, persister);
source.getPersistenceContext().checkUniqueness(key, object);
//get a snapshot
Object[] values = persister.getPropertyValues(object);
TypeHelper.deepCopy(values, persister.getPropertyTypes(), persister.getPropertyUpdateability(), values, source);
Object version = Versioning.getVersion(values, persister);
EntityEntry newEntry = source.getPersistenceContext().addEntity(object, (persister.isMutable() ? Status.MANAGED : Status.READ_ONLY), values, key, version, LockMode.NONE, true, persister, false);
new OnLockVisitor(source, id, object).process(object, persister);
persister.afterReassociate(object, source);
return newEntry;
}
use of org.hibernate.engine.spi.EntityKey in project hibernate-orm by hibernate.
the class DefaultDeleteEventListener method onDelete.
/**
* 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 (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);
}
}
use of org.hibernate.engine.spi.EntityKey in project hibernate-orm by hibernate.
the class DefaultEvictEventListener method onEvict.
/**
* Handle the given evict event.
*
* @param event The evict event to be handled.
*
* @throws HibernateException
*/
public void onEvict(EvictEvent event) throws HibernateException {
final Object object = event.getObject();
if (object == null) {
throw new NullPointerException("null passed to Session.evict()");
}
final EventSource source = event.getSession();
final PersistenceContext persistenceContext = source.getPersistenceContext();
if (object instanceof HibernateProxy) {
final LazyInitializer li = ((HibernateProxy) object).getHibernateLazyInitializer();
final Serializable id = li.getIdentifier();
if (id == null) {
throw new IllegalArgumentException("Could not determine identifier of proxy passed to evict()");
}
final EntityPersister persister = source.getFactory().getEntityPersister(li.getEntityName());
final EntityKey key = source.generateEntityKey(id, persister);
persistenceContext.removeProxy(key);
if (!li.isUninitialized()) {
final Object entity = persistenceContext.removeEntity(key);
if (entity != null) {
EntityEntry e = persistenceContext.removeEntry(entity);
doEvict(entity, key, e.getPersister(), event.getSession());
}
}
li.unsetSession();
} else {
EntityEntry e = persistenceContext.removeEntry(object);
if (e != null) {
persistenceContext.removeEntity(e.getEntityKey());
doEvict(object, e.getEntityKey(), e.getPersister(), source);
} else {
// see if the passed object is even an entity, and if not throw an exception
// this is different than legacy Hibernate behavior, but what JPA 2.1 is calling for
// with EntityManager.detach
EntityPersister persister = null;
final String entityName = persistenceContext.getSession().guessEntityName(object);
if (entityName != null) {
try {
persister = persistenceContext.getSession().getFactory().getEntityPersister(entityName);
} catch (Exception ignore) {
}
}
if (persister == null) {
throw new IllegalArgumentException("Non-entity object instance passed to evict : " + object);
}
}
}
}
Aggregations