Search in sources :

Example 1 with NonUniqueObjectException

use of org.hibernate.NonUniqueObjectException in project hibernate-orm by hibernate.

the class DefaultLoadEventListener method load.

/**
 * Performs the load of an entity.
 *
 * @param event The initiating load request event
 * @param persister The persister corresponding to the entity to be loaded
 * @param keyToLoad The key of the entity to be loaded
 * @param options The defined load options
 *
 * @return The loaded entity.
 *
 * @throws HibernateException
 */
private Object load(final LoadEvent event, final EntityPersister persister, final EntityKey keyToLoad, final LoadEventListener.LoadType options) {
    if (event.getInstanceToLoad() != null) {
        if (event.getSession().getPersistenceContext().getEntry(event.getInstanceToLoad()) != null) {
            throw new PersistentObjectException("attempted to load into an instance that was already associated with the session: " + MessageHelper.infoString(persister, event.getEntityId(), event.getSession().getFactory()));
        }
        persister.setIdentifier(event.getInstanceToLoad(), event.getEntityId(), event.getSession());
    }
    final Object entity = doLoad(event, persister, keyToLoad, options);
    boolean isOptionalInstance = event.getInstanceToLoad() != null;
    if (entity == null && (!options.isAllowNulls() || isOptionalInstance)) {
        event.getSession().getFactory().getEntityNotFoundDelegate().handleEntityNotFound(event.getEntityClassName(), event.getEntityId());
    } else if (isOptionalInstance && entity != event.getInstanceToLoad()) {
        throw new NonUniqueObjectException(event.getEntityId(), event.getEntityClassName());
    }
    return entity;
}
Also used : NonUniqueObjectException(org.hibernate.NonUniqueObjectException) PersistentObjectException(org.hibernate.PersistentObjectException)

Example 2 with NonUniqueObjectException

use of org.hibernate.NonUniqueObjectException in project hibernate-orm by hibernate.

the class MergeTest method testPersistThenMergeInSameTxnWithVersion.

@Test
public void testPersistThenMergeInSameTxnWithVersion() {
    Session s = openSession();
    Transaction tx = s.beginTransaction();
    VersionedEntity entity = new VersionedEntity("test", "test");
    s.persist(entity);
    s.merge(new VersionedEntity("test", "test-2"));
    try {
        // control operation...
        s.saveOrUpdate(new VersionedEntity("test", "test-3"));
        fail("saveOrUpdate() should fail here");
    } catch (NonUniqueObjectException expected) {
    // expected behavior
    }
    tx.commit();
    s.close();
    cleanup();
}
Also used : Transaction(org.hibernate.Transaction) NonUniqueObjectException(org.hibernate.NonUniqueObjectException) Session(org.hibernate.Session) Test(org.junit.Test)

Example 3 with NonUniqueObjectException

use of org.hibernate.NonUniqueObjectException in project hibernate-orm by hibernate.

the class MergeTest method testPersistThenMergeInSameTxnWithTimestamp.

@Test
public void testPersistThenMergeInSameTxnWithTimestamp() {
    Session s = openSession();
    Transaction tx = s.beginTransaction();
    TimestampedEntity entity = new TimestampedEntity("test", "test");
    s.persist(entity);
    s.merge(new TimestampedEntity("test", "test-2"));
    try {
        // control operation...
        s.saveOrUpdate(new TimestampedEntity("test", "test-3"));
        fail("saveOrUpdate() should fail here");
    } catch (NonUniqueObjectException expected) {
    // expected behavior
    }
    tx.commit();
    s.close();
    cleanup();
}
Also used : Transaction(org.hibernate.Transaction) NonUniqueObjectException(org.hibernate.NonUniqueObjectException) Session(org.hibernate.Session) Test(org.junit.Test)

Example 4 with NonUniqueObjectException

use of org.hibernate.NonUniqueObjectException in project hibernate-orm by hibernate.

the class AbstractSaveEventListener method performSave.

/**
 * Prepares the save call by checking the session caches for a pre-existing
 * entity and performing any lifecycle callbacks.
 *
 * @param entity The entity to be saved.
 * @param id The id by which to save the entity.
 * @param persister The entity's persister instance.
 * @param useIdentityColumn Is an identity column being used?
 * @param anything Generally cascade-specific information.
 * @param source The session from which the event originated.
 * @param requiresImmediateIdAccess does the event context require
 * access to the identifier immediately after execution of this method (if
 * not, post-insert style id generators may be postponed if we are outside
 * a transaction).
 *
 * @return The id used to save the entity; may be null depending on the
 *         type of id generator used and the requiresImmediateIdAccess value
 */
protected Serializable performSave(Object entity, Serializable id, EntityPersister persister, boolean useIdentityColumn, Object anything, EventSource source, boolean requiresImmediateIdAccess) {
    if (LOG.isTraceEnabled()) {
        LOG.tracev("Saving {0}", MessageHelper.infoString(persister, id, source.getFactory()));
    }
    final EntityKey key;
    if (!useIdentityColumn) {
        key = source.generateEntityKey(id, persister);
        Object old = source.getPersistenceContext().getEntity(key);
        if (old != null) {
            if (source.getPersistenceContext().getEntry(old).getStatus() == Status.DELETED) {
                source.forceFlush(source.getPersistenceContext().getEntry(old));
            } else {
                throw new NonUniqueObjectException(id, persister.getEntityName());
            }
        }
        persister.setIdentifier(entity, id, source);
    } else {
        key = null;
    }
    if (invokeSaveLifecycle(entity, persister, source)) {
        // EARLY EXIT
        return id;
    }
    return performSaveOrReplicate(entity, key, persister, useIdentityColumn, anything, source, requiresImmediateIdAccess);
}
Also used : EntityKey(org.hibernate.engine.spi.EntityKey) NonUniqueObjectException(org.hibernate.NonUniqueObjectException)

Aggregations

NonUniqueObjectException (org.hibernate.NonUniqueObjectException)4 Session (org.hibernate.Session)2 Transaction (org.hibernate.Transaction)2 Test (org.junit.Test)2 PersistentObjectException (org.hibernate.PersistentObjectException)1 EntityKey (org.hibernate.engine.spi.EntityKey)1