use of org.hibernate.TransientObjectException in project hibernate-orm by hibernate.
the class StatefulPersistenceContext method setEntityReadOnly.
private void setEntityReadOnly(Object entity, boolean readOnly) {
final EntityEntry entry = getEntry(entity);
if (entry == null) {
throw new TransientObjectException("Instance was not associated with this persistence context");
}
entry.setReadOnly(readOnly, entity);
hasNonReadOnlyEntities = hasNonReadOnlyEntities || !readOnly;
}
use of org.hibernate.TransientObjectException 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.TransientObjectException in project hibernate-orm by hibernate.
the class DefaultReplicateEventListener method onReplicate.
/**
* Handle the given replicate event.
*
* @param event The replicate event to be handled.
*
* @throws TransientObjectException An invalid attempt to replicate a transient entity.
*/
public void onReplicate(ReplicateEvent event) {
final EventSource source = event.getSession();
if (source.getPersistenceContext().reassociateIfUninitializedProxy(event.getObject())) {
LOG.trace("Uninitialized proxy passed to replicate()");
return;
}
Object entity = source.getPersistenceContext().unproxyAndReassociate(event.getObject());
if (source.getPersistenceContext().isEntryFor(entity)) {
LOG.trace("Ignoring persistent instance passed to replicate()");
//hum ... should we cascade anyway? throw an exception? fine like it is?
return;
}
EntityPersister persister = source.getEntityPersister(event.getEntityName(), entity);
// get the id from the object
/*if ( persister.isUnsaved(entity, source) ) {
throw new TransientObjectException("transient instance passed to replicate()");
}*/
Serializable id = persister.getIdentifier(entity, source);
if (id == null) {
throw new TransientObjectException("instance with null id passed to replicate()");
}
final ReplicationMode replicationMode = event.getReplicationMode();
final Object oldVersion;
if (replicationMode == ReplicationMode.EXCEPTION) {
//always do an INSERT, and let it fail by constraint violation
oldVersion = null;
} else {
//what is the version on the database?
oldVersion = persister.getCurrentVersion(id, source);
}
final boolean traceEnabled = LOG.isTraceEnabled();
if (oldVersion != null) {
if (traceEnabled) {
LOG.tracev("Found existing row for {0}", MessageHelper.infoString(persister, id, source.getFactory()));
}
/// HHH-2378
final Object realOldVersion = persister.isVersioned() ? oldVersion : null;
boolean canReplicate = replicationMode.shouldOverwriteCurrentVersion(entity, realOldVersion, persister.getVersion(entity), persister.getVersionType());
// else do nothing (don't even reassociate object!)
if (canReplicate) {
performReplication(entity, id, realOldVersion, persister, replicationMode, source);
} else if (traceEnabled) {
LOG.trace("No need to replicate");
}
//TODO: would it be better to do a refresh from db?
} else {
// no existing row - do an insert
if (traceEnabled) {
LOG.tracev("No existing row, replicating new instance {0}", MessageHelper.infoString(persister, id, source.getFactory()));
}
// prefer re-generation of identity!
final boolean regenerate = persister.isIdentifierAssignedByInsert();
final EntityKey key = regenerate ? null : source.generateEntityKey(id, persister);
performSaveOrReplicate(entity, key, persister, regenerate, replicationMode, source, true);
}
}
use of org.hibernate.TransientObjectException in project hibernate-orm by hibernate.
the class DefaultLockEventListener method onLock.
/**
* Handle the given lock event.
*
* @param event The lock event to be handled.
* @throws HibernateException
*/
public void onLock(LockEvent event) throws HibernateException {
if (event.getObject() == null) {
throw new NullPointerException("attempted to lock null");
}
if (event.getLockMode() == LockMode.WRITE) {
throw new HibernateException("Invalid lock mode for lock()");
}
if (event.getLockMode() == LockMode.UPGRADE_SKIPLOCKED) {
LOG.explicitSkipLockedLockCombo();
}
SessionImplementor source = event.getSession();
Object entity = source.getPersistenceContext().unproxyAndReassociate(event.getObject());
//TODO: if object was an uninitialized proxy, this is inefficient,
// resulting in two SQL selects
EntityEntry entry = source.getPersistenceContext().getEntry(entity);
if (entry == null) {
final EntityPersister persister = source.getEntityPersister(event.getEntityName(), entity);
final Serializable id = persister.getIdentifier(entity, source);
if (!ForeignKeys.isNotTransient(event.getEntityName(), entity, Boolean.FALSE, source)) {
throw new TransientObjectException("cannot lock an unsaved transient instance: " + persister.getEntityName());
}
entry = reassociate(event, entity, id, persister);
cascadeOnLock(event, persister, entity);
}
upgradeLock(entity, entry, event.getLockOptions(), event.getSession());
}
use of org.hibernate.TransientObjectException in project hibernate-orm by hibernate.
the class SessionImpl method getCurrentLockMode.
@Override
public LockMode getCurrentLockMode(Object object) throws HibernateException {
checkOpen();
checkTransactionSynchStatus();
if (object == null) {
throw new NullPointerException("null object passed to getCurrentLockMode()");
}
if (object instanceof HibernateProxy) {
object = ((HibernateProxy) object).getHibernateLazyInitializer().getImplementation(this);
if (object == null) {
return LockMode.NONE;
}
}
EntityEntry e = persistenceContext.getEntry(object);
if (e == null) {
throw new TransientObjectException("Given object not associated with the session");
}
if (e.getStatus() != Status.MANAGED) {
throw new ObjectDeletedException("The given object was deleted", e.getId(), e.getPersister().getEntityName());
}
return e.getLockMode();
}
Aggregations