Search in sources :

Example 1 with EntityExistsException

use of javax.persistence.EntityExistsException in project hibernate-orm by hibernate.

the class EntityManagerTest method testPersistExisting.

@Test
public void testPersistExisting() throws Exception {
    EntityManager em = getOrCreateEntityManager();
    em.getTransaction().begin();
    Wallet w = new Wallet();
    w.setBrand("Lacoste");
    w.setModel("Minimic");
    w.setSerial("0100202002");
    em.persist(w);
    w = new Wallet();
    w.setBrand("Lacoste");
    w.setModel("Minimic");
    w.setSerial("0100202002");
    try {
        em.persist(w);
    } catch (EntityExistsException eee) {
        //success
        if (em.getTransaction() != null) {
            em.getTransaction().rollback();
        }
        em.close();
        return;
    }
    try {
        em.getTransaction().commit();
        fail("Should have raised an exception");
    } catch (PersistenceException pe) {
    } finally {
        em.close();
    }
}
Also used : EntityManager(javax.persistence.EntityManager) PersistenceException(javax.persistence.PersistenceException) EntityExistsException(javax.persistence.EntityExistsException) Test(org.junit.Test)

Example 2 with EntityExistsException

use of javax.persistence.EntityExistsException in project cloudstack by apache.

the class CiscoVnmcElement method addCiscoAsa1000vResource.

@Override
public CiscoAsa1000vDevice addCiscoAsa1000vResource(AddCiscoAsa1000vResourceCmd cmd) {
    Long physicalNetworkId = cmd.getPhysicalNetworkId();
    CiscoAsa1000vDevice ciscoAsa1000vResource = null;
    PhysicalNetworkVO physicalNetwork = _physicalNetworkDao.findById(physicalNetworkId);
    if (physicalNetwork == null) {
        throw new InvalidParameterValueException("Could not find phyical network with ID: " + physicalNetworkId);
    }
    ciscoAsa1000vResource = new CiscoAsa1000vDeviceVO(physicalNetworkId, cmd.getManagementIp().trim(), cmd.getInPortProfile(), cmd.getClusterId());
    try {
        _ciscoAsa1000vDao.persist((CiscoAsa1000vDeviceVO) ciscoAsa1000vResource);
    } catch (EntityExistsException e) {
        throw new InvalidParameterValueException("An ASA 1000v appliance already exists with same configuration");
    }
    return ciscoAsa1000vResource;
}
Also used : CiscoAsa1000vDeviceVO(com.cloud.network.cisco.CiscoAsa1000vDeviceVO) InvalidParameterValueException(com.cloud.exception.InvalidParameterValueException) PhysicalNetworkVO(com.cloud.network.dao.PhysicalNetworkVO) CiscoAsa1000vDevice(com.cloud.network.cisco.CiscoAsa1000vDevice) EntityExistsException(javax.persistence.EntityExistsException)

Example 3 with EntityExistsException

use of javax.persistence.EntityExistsException in project cloudstack by apache.

the class ServiceOfferingDaoImpl method persistSystemServiceOffering.

@Override
@DB
public ServiceOfferingVO persistSystemServiceOffering(ServiceOfferingVO offering) {
    assert offering.getUniqueName() != null : "how are you going to find this later if you don't set it?";
    ServiceOfferingVO vo = findByName(offering.getUniqueName());
    if (vo != null) {
        // check invalid CPU speed in system service offering, set it to default value of 500 Mhz if 0 CPU speed is found
        if (vo.getSpeed() <= 0) {
            vo.setSpeed(500);
            update(vo.getId(), vo);
        }
        if (!vo.getUniqueName().endsWith("-Local")) {
            if (vo.getUseLocalStorage()) {
                vo.setUniqueName(vo.getUniqueName() + "-Local");
                vo.setName(vo.getName() + " - Local Storage");
                update(vo.getId(), vo);
            }
        }
        return vo;
    }
    try {
        return persist(offering);
    } catch (EntityExistsException e) {
        // Assume it's conflict on unique name
        return findByName(offering.getUniqueName());
    }
}
Also used : ServiceOfferingVO(com.cloud.service.ServiceOfferingVO) EntityExistsException(javax.persistence.EntityExistsException) DB(com.cloud.utils.db.DB)

Example 4 with EntityExistsException

use of javax.persistence.EntityExistsException in project API by ca-cwds.

the class CrossReportService method create.

/**
   * {@inheritDoc}
   * 
   * @see gov.ca.cwds.rest.services.CrudsService#create(gov.ca.cwds.rest.api.Request)
   */
@Override
public gov.ca.cwds.rest.api.domain.cms.CrossReport create(Request request) {
    assert request instanceof gov.ca.cwds.rest.api.domain.cms.CrossReport;
    gov.ca.cwds.rest.api.domain.cms.CrossReport crossReport = (gov.ca.cwds.rest.api.domain.cms.CrossReport) request;
    try {
        String lastUpdatedId = staffPersonIdRetriever.getStaffPersonId();
        CrossReport managed = new CrossReport(CmsKeyIdGenerator.cmsIdGenertor(lastUpdatedId), crossReport, lastUpdatedId);
        managed = crossReportDao.create(managed);
        return new gov.ca.cwds.rest.api.domain.cms.CrossReport(managed);
    } catch (EntityExistsException e) {
        LOGGER.info("CrossReport already exists : {}", crossReport);
        throw new ServiceException("CrossReport already exists : {}" + crossReport, e);
    }
}
Also used : CrossReport(gov.ca.cwds.data.persistence.cms.CrossReport) ServiceException(gov.ca.cwds.rest.services.ServiceException) EntityExistsException(javax.persistence.EntityExistsException)

Example 5 with EntityExistsException

use of javax.persistence.EntityExistsException in project API by ca-cwds.

the class LongTextService method create.

/**
   * {@inheritDoc}
   * 
   * @see gov.ca.cwds.rest.services.CrudsService#create(gov.ca.cwds.rest.api.Request)
   */
@Override
public PostedLongText create(Request request) {
    assert request instanceof gov.ca.cwds.rest.api.domain.cms.LongText;
    gov.ca.cwds.rest.api.domain.cms.LongText longText = (gov.ca.cwds.rest.api.domain.cms.LongText) request;
    try {
        String lastUpdatedId = staffPersonIdRetriever.getStaffPersonId();
        LongText managed = new LongText(CmsKeyIdGenerator.cmsIdGenertor(lastUpdatedId), longText, lastUpdatedId);
        managed = longTextDao.create(managed);
        return new PostedLongText(managed);
    } catch (EntityExistsException e) {
        LOGGER.info("LongText already exists : {}", longText);
        throw new ServiceException(e);
    }
}
Also used : EntityExistsException(javax.persistence.EntityExistsException) ServiceException(gov.ca.cwds.rest.services.ServiceException) PostedLongText(gov.ca.cwds.rest.api.domain.cms.PostedLongText) LongText(gov.ca.cwds.data.persistence.cms.LongText) PostedLongText(gov.ca.cwds.rest.api.domain.cms.PostedLongText)

Aggregations

EntityExistsException (javax.persistence.EntityExistsException)24 ServiceException (gov.ca.cwds.rest.services.ServiceException)13 SQLException (java.sql.SQLException)5 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)4 StaffPerson (gov.ca.cwds.data.persistence.cms.StaffPerson)4 PreparedStatement (java.sql.PreparedStatement)4 DB (com.cloud.utils.db.DB)3 PersistenceException (javax.persistence.PersistenceException)3 Ternary (com.cloud.utils.Ternary)2 ResultSet (java.sql.ResultSet)2 HashMap (java.util.HashMap)2 UUID (java.util.UUID)2 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)2 AttributeOverride (javax.persistence.AttributeOverride)2 EntityNotFoundException (javax.persistence.EntityNotFoundException)2 NoResultException (javax.persistence.NoResultException)2 NonUniqueResultException (javax.persistence.NonUniqueResultException)2 OptimisticLockException (javax.persistence.OptimisticLockException)2 InvalidParameterValueException (com.cloud.exception.InvalidParameterValueException)1 CiscoAsa1000vDevice (com.cloud.network.cisco.CiscoAsa1000vDevice)1