Example usage for javax.persistence EntityExistsException EntityExistsException

List of usage examples for javax.persistence EntityExistsException EntityExistsException

Introduction

In this page you can find the example usage for javax.persistence EntityExistsException EntityExistsException.

Prototype

public EntityExistsException(String message, Throwable cause) 

Source Link

Document

Constructs a new EntityExistsException exception with the specified detail message and cause.

Usage

From source file:org.castor.jpa.CastorEntityManager.java

/**
 * Make an entity instance managed and persistent.
 * //from   ww  w .  ja  v a 2s  .  c  o m
 * @param entity
 * @throws EntityExistsException
 *             if the entity already exists. (The EntityExistsException may
 *             be thrown when the persist operation is invoked, or the
 *             EntityExistsException or another PersistenceException may be
 *             thrown at flush or commit time.)
 * @throws IllegalStateException
 *             if this EntityManager has been closed.
 * @throws IllegalArgumentException
 *             if not an entity
 * @throws TransactionRequiredException
 *             if invoked on a container-managed entity manager of type
 *             PersistenceContextType.TRANSACTION and there is no
 *             transaction.
 */
public void persist(Object entity) {
    // Check whether the entity manager is open.
    verifyOpenEntityManager();
    // Check whether a transaction is running.
    verifyRunningTransaction();

    if (this.context.contains(entity)) {
        // The entity is already managed within this persistence context.
        return;
    }

    try {

        // Create the entity.
        this.database.create(entity);

        // Add the entity to the set of managed entities.
        this.context.manage(entity);

    } catch (ClassNotPersistenceCapableException e) {
        log.error("Entity of type >" + entity.getClass().getName() + "< is not valid entity type.", e);
        throw new IllegalArgumentException(
                "Entity of type >" + entity.getClass().getName() + "< is not valid entity type.", e);
    } catch (DuplicateIdentityException e) {
        log.error("Entity of type " + entity.getClass().getName() + " already exists.");
        throw new EntityExistsException("Entity of type " + entity.getClass().getName() + " already exists.",
                e);
    } catch (org.exolab.castor.jdo.PersistenceException e) {
        throw new PersistenceException(e);
    }
}