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:org.nuxeo.ecm.platform.ec.placeful.PlacefulServiceImpl.java

public void removeAnnotationListByParamMap(EntityManager em, Map<String, Object> paramMap, String name) {

    List<Annotation> annotationsToRemove = getAnnotationListByParamMap(em, paramMap, name);
    if (annotationsToRemove != null && !annotationsToRemove.isEmpty()) {
        for (Annotation anno : annotationsToRemove) {
            if (anno != null) {
                em.remove(anno);
            }/*from  ww  w .j  av  a  2  s . c  o  m*/
        }
    }
}

From source file:ec.edu.chyc.manejopersonal.controller.PersonaJpaController.java

public void destroy(Long id) throws Exception {
    EntityManager em = null;
    try {//from w w  w .  jav  a  2s.  c  o m
        em = getEntityManager();
        em.getTransaction().begin();
        em.remove(id);
        em.getTransaction().commit();
    } finally {
        if (em != null) {
            em.close();
        }
    }
}

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

public synchronized void deleteWidgetData(final Widget widget) throws ProviderException {
    if (widget == null) {
        throw new ProviderException("Widget is undefined");
    }/*w  w  w.  ja  v a 2  s  . c  o m*/

    try {
        getPersistenceProvider().run(true, new RunVoid() {
            public void runWith(EntityManager em) {
                Query query = em.createNamedQuery("Data.findByWidget");
                query.setParameter("widgetUid", widget.getUid());
                for (Object dataEntity : query.getResultList()) {
                    em.remove(dataEntity);
                }
            }
        });
    } catch (ClientException e) {
        throw new ProviderException(e);
    }

}

From source file:org.opencastproject.adminui.usersettings.UserSettingsService.java

/**
 * Delete a user setting by using a unique id to find it.
 *
 * @param id/*from  ww  w .j  ava 2s.c  om*/
 *          The unique id for the user setting.
 * @throws UserSettingsServiceException
 */
public void deleteUserSetting(long id) throws UserSettingsServiceException {
    EntityManager em = null;
    EntityTransaction tx = null;
    try {
        em = emf.createEntityManager();
        UserSettingDto userSettingsDto = em.find(UserSettingDto.class, id);
        tx = em.getTransaction();
        tx.begin();
        em.remove(userSettingsDto);
        tx.commit();
    } catch (Exception e) {
        logger.error("Could not delete user setting '%d': %s", id, ExceptionUtils.getStackTrace(e));
        if (tx.isActive())
            tx.rollback();
        throw new UserSettingsServiceException(e);
    } finally {
        if (em != null)
            em.close();
    }
}

From source file:eu.clarin.cmdi.virtualcollectionregistry.VirtualCollectionRegistryMaintenanceImpl.java

protected void purgeDeletedCollections(EntityManager em, final Date nowDatePurge) {
    em.getTransaction().begin();//from w ww . ja v  a  2s.c o  m
    TypedQuery<VirtualCollection> q = em.createNamedQuery("VirtualCollection.findAllByState",
            VirtualCollection.class);
    q.setParameter("state", VirtualCollection.State.DELETED);
    q.setParameter("date", nowDatePurge);
    q.setLockMode(LockModeType.PESSIMISTIC_WRITE);
    for (VirtualCollection vc : q.getResultList()) {
        vc.setState(VirtualCollection.State.DEAD);
        em.remove(vc);
        logger.debug("purged virtual collection (id={})", vc.getId());
    }
    em.getTransaction().commit();
}

From source file:org.rhq.enterprise.server.drift.DriftServerTest.java

protected void deleteEntity(Class<?> clazz, String name, EntityManager em) {
    try {//from  ww  w  . j  a va  2 s .  c om
        Object entity = em
                .createQuery(
                        "select entity from " + clazz.getSimpleName() + " entity where entity.name = :name")
                .setParameter("name", name).getSingleResult();
        if (clazz.equals(Resource.class)) {
            ResourceTreeHelper.deleteResource(em, (Resource) entity);
        } else {
            em.remove(entity);
        }
    } catch (NoResultException e) {
        // we can ignore no results because this code will run when the db
        // is empty and we expect no results in that case
    } catch (NonUniqueResultException e) {
        // we will fail here to let the person running the test know that
        // the database may not be in a consistent state
        fail("Purging " + name + " failed. Expected to find one instance of " + clazz.getSimpleName()
                + " but found more than one. The database may not be in a consistent state.");
    }
}

From source file:org.opencastproject.kernel.security.persistence.OrganizationDatabaseImpl.java

/**
 * @see org.opencastproject.kernel.security.persistence.OrganizationDatabase#deleteOrganization(java.lang.String)
 *///from w ww  .ja va  2s.c  o  m
@Override
public void deleteOrganization(String orgId) throws OrganizationDatabaseException, NotFoundException {
    EntityManager em = null;
    EntityTransaction tx = null;
    try {
        em = emf.createEntityManager();
        tx = em.getTransaction();
        tx.begin();
        JpaOrganization organization = getOrganizationEntity(orgId);
        if (organization == null)
            throw new NotFoundException("Organization " + orgId + " does not exist");

        em.remove(organization);
        tx.commit();
    } catch (NotFoundException e) {
        throw e;
    } catch (Exception e) {
        logger.error("Could not delete organization: {}", e.getMessage());
        if (tx.isActive()) {
            tx.rollback();
        }
        throw new OrganizationDatabaseException(e);
    } finally {
        if (em != null)
            em.close();
    }
}

From source file:kirchnerei.note.model.DataService.java

public boolean removeNote(Long id) {
    if (NumberUtils.isEmpty(id)) {
        return false;
    }//w w w  .  j a v  a 2 s . com
    EntityManager em = null;
    EntityTransaction tx = null;
    try {
        em = entityService.get();
        tx = em.getTransaction();
        tx.begin();
        Note note = em.find(Note.class, id);
        if (note == null) {
            return false;
        }
        Category category = note.getCategory();
        category.getNotes().remove(note);
        note.setCategory(null);
        em.remove(note);
        if (category.getNotes().isEmpty()) {
            LogUtils.debug(log, "empty category '%s', then remove it", category.getTitle());
            em.remove(category);
        }
        EntityService.commit(tx);
        return true;
    } catch (Exception e) {
        EntityService.rollback(tx);
        return false;
    } finally {
        EntityService.close(em);
    }
}

From source file:nl.b3p.viewer.admin.stripes.LayoutManagerActionBean.java

public Resolution removeComponent() {

    EntityManager em = Stripersist.getEntityManager();

    try {//  w w  w. jav  a 2  s . c om
        component = (ConfiguredComponent) em
                .createQuery("from ConfiguredComponent where application = :application and name = :name")
                .setParameter("application", application).setParameter("name", name).getSingleResult();

        em.remove(component);
    } catch (NoResultException e) {
    }
    em.getTransaction().commit();
    return new ForwardResolution("/WEB-INF/jsp/application/layoutmanager.jsp");
}

From source file:org.opencastproject.comments.persistence.CommentDatabaseImpl.java

@Override
public void deleteComment(long commentId) throws CommentDatabaseException, NotFoundException {
    EntityManager em = emf.createEntityManager();
    EntityTransaction tx = em.getTransaction();
    try {/*  w  ww  .j a va2 s . c o m*/
        tx.begin();
        Option<CommentDto> dto = CommentDatabaseUtils.find(Option.some(commentId), em, CommentDto.class);
        if (dto.isNone())
            throw new NotFoundException("Comment with ID " + commentId + " does not exist");

        CommentDatabaseUtils.deleteReplies(dto.get().getReplies(), em);

        dto.get().setReplies(new ArrayList<CommentReplyDto>());
        em.remove(dto.get());
        tx.commit();
    } catch (NotFoundException e) {
        throw e;
    } catch (Exception e) {
        logger.error("Could not delete comment: {}", ExceptionUtils.getStackTrace(e));
        if (tx.isActive())
            tx.rollback();

        throw new CommentDatabaseException(e);
    } finally {
        if (em != null)
            em.close();
    }
}