Example usage for javax.persistence EntityManager remove

List of usage examples for javax.persistence EntityManager remove

Introduction

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

Prototype

public void remove(Object entity);

Source Link

Document

Remove the entity instance.

Usage

From source file:cz.fi.muni.pa165.daoImpl.TroopDAOImpl.java

@Override
public void removeTroop(Troop troop) throws IllegalArgumentException {
    if (troop == null) {
        throw new IllegalArgumentException("Troop can't be null.");
    }//from  w  w w. ja v a 2 s.  com
    if (troop.getId() == null) {
        throw new IllegalArgumentException("Troop is not present in DB.");
    }
    EntityManager em = emf.createEntityManager();
    em.getTransaction().begin();
    Troop present = em.find(Troop.class, troop.getId());
    em.getTransaction().commit();

    if (present == null) {
        throw new IllegalArgumentException("Troop is not present in DB.");
    } else {
        em.getTransaction().begin();
        em.remove(em.contains(troop) ? troop : em.merge(troop));
        em.getTransaction().commit();
    }
    em.close();
}

From source file:org.apache.roller.planet.business.jpa.JPAPersistenceStrategy.java

/**
 * Remove object from persistence storage.
 * @param pos the persistent objects to remove
 * @throws org.apache.roller.planet.PlanetException on any error
 *///  ww  w.ja v a2  s . com
public void removeAll(Collection pos) throws PlanetException {
    EntityManager em = getEntityManager(true);
    for (Iterator iterator = pos.iterator(); iterator.hasNext();) {
        Object obj = iterator.next();
        em.remove(obj);
    }
}

From source file:com.epam.training.taranovski.web.project.repository.implementation.VacancyRepositoryImplementation.java

@Override
public boolean delete(Vacancy vacancy) {
    EntityManager em = entityManagerFactory.createEntityManager();
    boolean success = true;
    Vacancy managedVacancy = null;// ww  w .j a v a  2 s.c o  m
    try {
        em.getTransaction().begin();
        managedVacancy = em.merge(vacancy);
        em.remove(managedVacancy);
        em.getTransaction().commit();

        success = true;
    } catch (RuntimeException e) {
        Logger.getLogger(VacancyRepositoryImplementation.class.getName()).info(e);
    } finally {
        if (em.getTransaction().isActive()) {
            em.getTransaction().rollback();
            success = false;
        }
        em.close();
    }

    return success;
}

From source file:com.medigy.persist.EntitySaveListener.java

public void deleteEntityList(final EntityManager manager) {
    while (!entityList.empty()) {
        final EntityInfo obj = (EntityInfo) entityList.pop();
        if (log.isInfoEnabled())
            log.info("Deleteing... " + obj.getEntityClass() + ". (id = " + obj.getId() + ")\n");
        final Object entity = manager.find(obj.getEntityClass(), obj.getId());
        if (entity != null)
            manager.remove(entity);
    }/*w w  w.java 2s. c  o m*/
}

From source file:org.apereo.portal.portlet.dao.jpa.JpaPortletEntityDao.java

@Override
@PortalTransactional//from w  ww .  ja v  a  2 s  . c o  m
public void deletePortletEntity(IPortletEntity portletEntity) {
    Validate.notNull(portletEntity, "portletEntity can not be null");

    final IPortletEntity persistentPortletEntity;
    final EntityManager entityManager = this.getEntityManager();
    if (entityManager.contains(portletEntity)) {
        persistentPortletEntity = portletEntity;
    } else {
        persistentPortletEntity = entityManager.merge(portletEntity);
    }

    entityManager.remove(persistentPortletEntity);
}

From source file:de.iai.ilcd.model.dao.AbstractDao.java

/**
 * Default remove: bring back to persistence context if required and delete
 * //from w  w  w . j a va2s .  com
 * @param obj
 *            object to remove
 * @return remove object
 * @throws Exception
 *             on errors (transaction is being rolled back)
 */
public T remove(T obj) throws Exception {
    if (obj == null) {
        return null;
    }
    EntityManager em = PersistenceUtil.getEntityManager();
    EntityTransaction t = em.getTransaction();

    try {
        t.begin();
        T tmp = em.contains(obj) ? obj : em.merge(obj);
        em.remove(tmp);
        t.commit();
        return tmp;
    } catch (Exception e) {
        t.rollback();
        throw e;
    }
}

From source file:it.infn.ct.futuregateway.apiserver.v1.TaskService.java

/**
 * Removes the task. Task is deleted and all the associated activities and
 * or files removed.//  ww w . jav a  2  s .  c  o m
 *
 * @param id Id of the task to remove
 */
@DELETE
public final void deleteTask(@PathParam("id") final String id) {
    Task task;
    EntityManager em = getEntityManager();
    try {
        task = em.find(Task.class, id);
        if (task == null) {
            throw new NotFoundException();
        }
        EntityTransaction et = em.getTransaction();
        try {
            et.begin();
            em.remove(task);
            et.commit();
        } catch (RuntimeException re) {
            if (et != null && et.isActive()) {
                et.rollback();
            }
            log.error(re);
            log.error("Impossible to remove the task");
            em.close();
            throw new InternalServerErrorException("Errore to remove " + "the task " + id);
        }
        try {
            Storage store = getStorage();
            store.removeAllFiles(Storage.RESOURCE.TASKS, id);
        } catch (IOException ex) {
            log.error("Impossible to remove the directory associated with " + "the task " + id);
        }
    } catch (IllegalArgumentException re) {
        log.error("Impossible to retrieve the task list");
        log.error(re);
        throw new BadRequestException("Task '" + id + "' has a problem!");
    } finally {
        em.close();
    }
}

From source file:org.nuxeo.theme.webwidgets.providers.PersistentProvider.java

public synchronized void removeWidget(final Widget widget) throws ProviderException {
    if (widget == null) {
        throw new ProviderException("Widget is undefined");
    }//w w  w  .  ja v  a2s  .c o m
    WidgetEntity widgetEntity = (WidgetEntity) widget;
    final int id = widgetEntity.getId();
    try {
        getPersistenceProvider().run(true, new RunVoid() {
            public void runWith(EntityManager em) {
                WidgetEntity w = em.getReference(WidgetEntity.class, id);
                em.remove(w);
            }
        });
    } catch (ClientException e) {
        throw new ProviderException(e);
    }
    List<Widget> widgets = getWidgets(widgetEntity.getRegion());
    reorderWidgets(widgets);
}

From source file:fr.mby.opa.picsimpl.dao.DbPictureDao.java

@Override
public void deletePicture(final Picture picture) throws PictureNotFoundException {
    Assert.notNull(picture, "No Picture supplied !");
    Assert.notNull(picture.getId(), "Id should be set for delete !");

    new TxCallback(this.getEmf()) {

        @Override// w w  w  .j  a v a  2s  . c o m
        protected void executeInTransaction(final EntityManager em) {
            final Picture managedPicture = em.find(Picture.class, picture.getId(), LockModeType.WRITE);
            if (managedPicture == null) {
                throw new PictureNotFoundException();
            }

            em.remove(picture);
            em.lock(picture, LockModeType.NONE);
        }
    };
}

From source file:nl.b3p.kaartenbalie.struts.ReportingAction.java

public ActionForward delete(ActionMapping mapping, DynaValidatorForm dynaForm, HttpServletRequest request,
        HttpServletResponse response) throws Exception {
    log.debug("Getting entity manager ......");
    EntityManager em = getEntityManager();
    String[] deleteList = (String[]) dynaForm.get("deleteReport");
    for (int i = 0; i < deleteList.length; i++) {
        Integer id = FormUtils.StringToInteger(deleteList[i]);
        Report report = (Report) em.find(Report.class, id);
        if (report != null) {
            em.remove(report);
        }/*w  w  w.  j  av a 2s.co m*/
        em.flush();
    }

    dynaForm.initialize(mapping);
    prepareMethod(dynaForm, request, LIST, EDIT);
    addDefaultMessage(mapping, request, ACKNOWLEDGE_MESSAGES);
    return getDefaultForward(mapping, request);
}