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:facades.PersonFacadeDB.java

@Override
public Person delete(Integer id) throws NotFoundException {
    EntityManagerFactory emf = Persistence.createEntityManagerFactory(persistenceFileName);
    EntityManager em = emf.createEntityManager();
    EntityTransaction transaction = em.getTransaction();
    transaction.begin();// w  w  w .j av  a  2  s  .  com

    Person p = em.find(Person.class, id);

    if (p == null) {
        transaction.rollback();
        throw new NotFoundException("No person for the given id");
    } else {
        em.remove(p);
        transaction.commit();
    }

    return p;
}

From source file:net.anthonychaves.bookmarks.service.UserService.java

public User deleteBookmark(User user, String bookmarkId) {
    EntityManager em = emf.createEntityManager();
    em.getTransaction().begin();//from   ww w.  j a v a 2 s. c  om
    Bookmark bookmark = (Bookmark) em.find(Bookmark.class, bookmarkId);
    if (user.getId() != bookmark.getUser().getId()) {
        throw new RuntimeException("user ids don't match when deleting a bookmark");
    }
    user = (User) em.find(User.class, user.getId());
    user.getBookmarks().remove(bookmark);
    em.remove(bookmark);
    em.getTransaction().commit();

    return user;
}

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

@Override
@PortalTransactional/* ww  w  . j  a v a2s. c o  m*/
public void deletePortletDefinition(IPortletDefinition definition) {
    Validate.notNull(definition, "definition can not be null");

    final IPortletDefinition persistentPortletDefinition;
    final EntityManager entityManager = this.getEntityManager();
    if (entityManager.contains(definition)) {
        persistentPortletDefinition = definition;
    } else {
        persistentPortletDefinition = entityManager.merge(definition);
    }

    entityManager.remove(persistentPortletDefinition);
}

From source file:fr.mby.portal.coreimpl.auth.DbPortalUserAuthenticationProvider.java

/** Init the DB with default users : user:user456 & admin:admin456 */
protected void initDefaultUsers() {
    final PortalUser user = this.buildPortalUser("user", "user456");
    final PortalUser admin = this.buildPortalUser("admin", "admin456");

    final EntityManager portalUserEm = this.portalUserEmf.createEntityManager();

    // Remove transaction
    portalUserEm.getTransaction().begin();

    final PortalUser foundAdmin = this.findPortalUserByLogin(portalUserEm, "admin");
    if (foundAdmin != null) {
        portalUserEm.remove(foundAdmin);
    }//from   w w  w  . ja  v a2 s  .co  m

    final PortalUser foundUser = this.findPortalUserByLogin(portalUserEm, "user");
    if (foundUser != null) {
        portalUserEm.remove(foundUser);
    }

    portalUserEm.getTransaction().commit();

    // Insert transaction
    portalUserEm.getTransaction().begin();

    portalUserEm.persist(user);
    portalUserEm.persist(admin);

    portalUserEm.getTransaction().commit();

    portalUserEm.close();
}

From source file:org.apereo.portal.persondir.dao.jpa.JpaLocalAccountDaoImpl.java

@Override
@PortalTransactional/* w ww.j  a  v a 2 s. co  m*/
public void deleteAccount(ILocalAccountPerson account) {
    Validate.notNull(account, "definition can not be null");

    final EntityManager entityManager = this.getEntityManager();

    final ILocalAccountPerson persistentAccount;
    if (entityManager.contains(account)) {
        persistentAccount = account;
    } else {
        persistentAccount = entityManager.merge(account);
    }

    entityManager.remove(persistentAccount);
}

From source file:de.zib.gndms.infra.model.GridEntityModelHandler.java

/**
 * Removes a resource's model from the persistent store
 *
 * @param emParam the EntityManager to be used or null for an EM from this handler's system
 * @param resource/*from  w  w  w . jav  a 2  s. c o m*/
 * @throws NoSuchResourceException if no model exists
 */
final public @NotNull M removeModel(final EntityManager emParam, final @NotNull R resource) {
    return txRun(emParam, new Function<EntityManager, M>() {
        public M apply(@com.google.common.base.Nullable @NotNull EntityManager em) {
            try {
                final @NotNull M model = loadModel(em, resource);
                em.remove(model);
                return model;
            } catch (ResourceException e) {
                throw new RuntimeException(e);
            }
        }
    });
}

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

/**
 * Default remove: bring back to persistence context if required and delete
 * /*from  ww w .  ja  v a 2s. c om*/
 * @param objs
 *            objects to remove
 * @return removed objects
 * @throws Exception
 *             on errors (transaction is being rolled back)
 */
public Collection<T> remove(Collection<T> objs) throws Exception {
    if (objs == null || objs.isEmpty()) {
        return null;
    }
    Collection<T> res = new ArrayList<T>();
    EntityManager em = PersistenceUtil.getEntityManager();
    EntityTransaction t = em.getTransaction();

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

From source file:fr.amapj.service.services.saisiepermanence.PermanenceService.java

/**
 * Permet de supprimer une distribution//  w  ww .j  a v  a  2 s  .  c om
 * Ceci est fait dans une transaction en ecriture
 */
@DbWrite
public void deleteDistribution(final Long id) {
    EntityManager em = TransactionHelper.getEm();

    DatePermanence d = em.find(DatePermanence.class, id);

    List<DatePermanenceUtilisateur> dus = getAllDateDistriUtilisateur(em, d);
    for (DatePermanenceUtilisateur du : dus) {
        em.remove(du);
    }

    em.remove(d);
}

From source file:com.yahoo.sql4d.indexeragent.meta.DBHandler.java

private void addUpdateDeleteEntity(Object entity, Action action) {
    EntityManager em = getEntityManager();
    try {//  w w w.j a va2s .c  o  m
        em.getTransaction().begin();
        switch (action) {
        case ADD:
            em.persist(entity);
            break;
        case UPDATE:
            em.merge(entity);
            break;
        case DELETE:
            em.remove(entity);
            break;
        }
    } catch (RuntimeException e) {
        log.error("Something wrong persisting/merging/removing entity {}, so rolling back . Exception is {}",
                entity, ExceptionUtils.getStackTrace(e));
        em.getTransaction().rollback();
    } finally {
        if (em.getTransaction().isActive()) {
            em.getTransaction().commit();
        }
        em.close();
    }
}

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

@Override
@Transactional//from w w  w.ja va  2  s . c  o m
public void deleteMultimedia(Multimedia multimedia) {
    Validate.notNull(multimedia, "multimedia can not be null");

    final EntityManager entityManager = this.getEntityManager();

    final MultimediaImpl multimediaImpl = entityManager.find(MultimediaImpl.class,
            multimedia.getMultimediaId());

    final ConferenceUser creator = multimediaImpl.getCreator();
    final ConferenceUserImpl creatorImpl = this.conferenceUserDao.getUser(creator.getUserId());
    creatorImpl.getMultimedias().remove(multimediaImpl);

    entityManager.remove(multimediaImpl);
    //entityManager.remove(creatorImpl);      
}