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.batoo.jpa.benchmark.BenchmarkTest.java

private void doBenchmarkRemove(final EntityManager em, final Person person) {
    em.remove(person);

    this.close(em);
}

From source file:com.busimu.core.dao.impl.UserMngDaoPolicyJpaImpl.java

/**
 * {@inheritDoc}/*from www.  j  a  v a 2  s . c  om*/
 */
@Override
public User activateUser(User user, String licenseCode) {
    EntityManager em = ((EntityManagerHolder) TransactionSynchronizationManager.getResource(emf))
            .getEntityManager();
    EntityTransaction tx = em.getTransaction();
    License l = getLicenseByCode(em, licenseCode);
    if (l != null) {
        if (!tx.isActive()) {
            tx.begin();
        }
        user.setType(User.Type.valueOf(l.getType().name()));
        user.setStatus(User.Status.ACTIVE);
        user = em.merge(user);
        em.remove(l);
        tx.commit();
    }
    return user;
}

From source file:in.bookmylab.jpa.JpaDAO.java

/**
 * @param xrd/*ww  w.  j ava  2  s.  com*/
 * @param em
 */
private void fixAnalysisModesAndResourceType(ResourceBooking booking, EntityManager em, boolean isUpdate) {
    // Analysis modes deletion
    if (booking.analysisModes != null) {
        for (Iterator<AnalysisMode> itr = booking.analysisModes.iterator(); itr.hasNext();) {
            AnalysisMode a = itr.next();
            if (a.deleted) {
                if (isUpdate) {
                    em.remove(em.merge(a));
                }
                itr.remove(); // Remove from collection
            } else {
                // Attach resource type
                Query q = em.createNamedQuery("ResourceType.findByCode");
                ResourceType rt = (ResourceType) q
                        .setParameter("code", StringUtils.upperCase(a.resourceType.code)).getSingleResult();
                a.resourceType = rt;
                if (isUpdate) {
                    em.merge(a);
                }
            }
        }
    }
}

From source file:org.opencastproject.themes.persistence.ThemesServiceDatabaseImpl.java

@Override
public void deleteTheme(long id) throws ThemesServiceDatabaseException, NotFoundException {
    EntityManager em = null;
    EntityTransaction tx = null;/*w w  w  . ja  va 2 s. c om*/
    try {
        em = emf.createEntityManager();
        ThemeDto themeDto = getThemeDto(id, em);
        if (themeDto == null)
            throw new NotFoundException("No theme with id=" + id + " exists");

        tx = em.getTransaction();
        tx.begin();
        em.remove(themeDto);
        tx.commit();
        messageSender.sendObjectMessage(ThemeItem.THEME_QUEUE, MessageSender.DestinationType.Queue,
                ThemeItem.delete(id));
    } catch (NotFoundException e) {
        throw e;
    } catch (Exception e) {
        logger.error("Could not delete theme '{}': {}", id, ExceptionUtils.getStackTrace(e));
        if (tx.isActive())
            tx.rollback();
        throw new ThemesServiceDatabaseException(e);
    } finally {
        if (em != null)
            em.close();
    }
}

From source file:fr.xebia.demo.wicket.blog.service.GenericService.java

public void delete(T entity) throws ServiceException {
    try {/*  w ww.  j  a v a 2s  .  c o  m*/
        EntityManager entityManager = currentEntityManager();
        entityManager.getTransaction().begin();

        Serializable objectId = getObjectId(entity);
        if (objectId == null) {
            throw new ServiceException("Entity has no id");
        }
        T loadedEntity = entityManager.find(getObjectClass(), objectId);
        if (loadedEntity == null) {
            throw new ServiceException("Entity referenced by id " + objectId + " does not exist");
        }
        entityManager.remove(loadedEntity);

        commitTransaction();
    } catch (PersistenceException e) {
        logger.error(e.getCause(), e);
        rollbackTransaction();
        throw new ServiceException("Can't delete object", e);
    } finally {
        closeEntityManager();
    }
}

From source file:org.jasig.portlet.blackboardvcportlet.dao.impl.UserSessionUrlDaoImpl.java

@Override
@Transactional//from   w  w w .j  a  va  2s. co  m
public void deleteOldSessionUrls(Session session, ConferenceUser user) {
    final EntityManager entityManager = this.getEntityManager();

    //fetch session
    SessionImpl sessionFromDb = sessionDao.getSession(session.getSessionId());
    //fetch user
    ConferenceUserImpl userFromDb = blackboardUserDao.getUser(user.getUserId());
    //assert they are valid
    Validate.notNull(sessionFromDb);
    Validate.notNull(userFromDb);

    final NaturalIdQuery<UserSessionUrlImpl> query = this.createNaturalIdQuery(UserSessionUrlImpl.class);
    query.using(UserSessionUrlImpl_.session, sessionFromDb);
    query.using(UserSessionUrlImpl_.user, userFromDb);

    UserSessionUrlImpl url = query.load();

    entityManager.remove(url);
    entityManager.flush();
}

From source file:org.opencastproject.scheduler.impl.persistence.SchedulerServiceDatabaseImpl.java

@Override
public void deleteEvent(long eventId) throws NotFoundException, SchedulerServiceDatabaseException {
    EntityManager em = null;
    EntityTransaction tx = null;/*from w  ww . j  av a2  s  .co m*/
    try {
        em = emf.createEntityManager();
        tx = em.getTransaction();
        tx.begin();
        EventEntity entity = em.find(EventEntity.class, eventId);
        if (entity == null) {
            throw new NotFoundException("Event with ID " + eventId + " does not exist");
        }
        em.remove(entity);
        tx.commit();
    } catch (Exception e) {
        if (tx.isActive()) {
            tx.rollback();
        }
        if (e instanceof NotFoundException) {
            throw (NotFoundException) e;
        }
        logger.error("Could not delete series: {}", e.getMessage());
        throw new SchedulerServiceDatabaseException(e);
    } finally {
        if (em != null)
            em.close();
    }
}

From source file:com.espirit.moddev.examples.uxbridge.newsdrilldown.test.CommandITCase.java

/**
 * Before test.//from  w ww  .j a  va2s  .c  o  m
 *
 * @throws Exception the exception
 */
@Before
public void beforeTest() throws Exception {
    EntityManager em = emf.createEntityManager();
    try {
        EntityTransaction et = em.getTransaction();
        et.begin();
        Query query = em.createQuery(new StringBuilder().append("SELECT x FROM news x").toString());
        List<News> newsList = query.getResultList();
        for (News news : newsList) {
            em.remove(news);
        }

        query = em.createQuery(new StringBuilder().append("SELECT x FROM category x").toString());
        List<NewsCategory> catList = query.getResultList();
        for (NewsCategory temp : catList) {
            em.remove(temp);
        }

        query = em.createQuery(new StringBuilder().append("SELECT x FROM metaCategory x").toString());
        List<NewsMetaCategory> metaList = query.getResultList();
        for (NewsMetaCategory temp : metaList) {
            em.remove(temp);
        }
        et.commit();
    } finally {
        em.close();
    }

}

From source file:eu.forgestore.ws.impl.FStoreJpaController.java

public void deleteProduct(int id) {
    EntityManager entityManager = entityManagerFactory.createEntityManager();
    Product p = entityManager.find(Product.class, id);

    EntityTransaction entityTransaction = entityManager.getTransaction();

    entityTransaction.begin();//from   w  w  w  .java  2 s  .  c o m
    entityManager.remove(p);
    entityTransaction.commit();
}

From source file:eu.forgestore.ws.impl.FStoreJpaController.java

public void deleteCategory(int catid) {

    EntityManager entityManager = entityManagerFactory.createEntityManager();
    Category c = entityManager.find(Category.class, catid);

    EntityTransaction entityTransaction = entityManager.getTransaction();
    entityTransaction.begin();//from w w w.ja va 2  s.c om
    entityManager.remove(c);
    entityTransaction.commit();

}