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:com.sdl.odata.datasource.jpa.JPADataSource.java

@Override
public void delete(ODataUri uri, EntityDataModel entityDataModel) throws ODataException {
    Option<Object> entity = extractEntityWithKeys(uri, entityDataModel);

    if (entity.isDefined()) {
        Object jpaEntity = entityMapper.convertODataEntityToDS(entity.get(), entityDataModel);
        if (jpaEntity != null) {
            EntityManager entityManager = getEntityManager();
            EntityTransaction transaction = entityManager.getTransaction();
            try {
                transaction.begin();/*w  w  w .jav  a  2s .  c  o  m*/

                Object attached = entityManager.merge(jpaEntity);
                entityManager.remove(attached);
            } catch (PersistenceException e) {
                LOG.error("Could not remove entity: {}", entity);
                throw new ODataDataSourceException("Could not remove entity", e);
            } finally {
                if (transaction.isActive()) {
                    transaction.commit();
                } else {
                    transaction.rollback();
                }
            }
        } else {
            throw new ODataDataSourceException("Could not remove entity, could not be loaded");
        }
    }
}

From source file:org.synyx.hades.dao.orm.GenericJpaDao.java

public void delete(final T entity) {

    EntityManager em = getEntityManager();
    em.remove(em.contains(entity) ? entity : em.merge(entity));
}

From source file:info.san.books.app.model.listener.LivreListener.java

@EventHandler
public void handle(LivreDeletedEvent e) {
    EntityManager em = Persistence.getInstance().createEntityManager();

    EntityTransaction t = em.getTransaction();

    t.begin();/*  www.  j a  v  a2s .c om*/

    LivreEntry entry = em.getReference(LivreEntry.class, e.getIsbn());
    em.remove(entry);

    t.commit();
}

From source file:org.apache.openjpa.jdbc.meta.strats.AbstractLobTest.java

public void testDelete() {
    insert(newLobEntity(createLobData(), 1));
    EntityManager em = emf.createEntityManager();
    em.getTransaction().begin();/*from  w w  w. j  a  v  a 2s .  c o  m*/
    LobEntity entity = (LobEntity) em.find(getLobEntityClass(), 1);
    em.remove(entity);
    em.getTransaction().commit();
    em.close();
    em = emf.createEntityManager();
    em.getTransaction().begin();
    Query q = em.createQuery(getSelectQuery());
    assertEquals(0, q.getResultList().size());
    em.getTransaction().commit();
    em.close();
}

From source file:ejb.bean.UsuarioDAOJPAImplBean.java

/**Mtodo para a remoo de usurio.
 * @author Richel Sensineli// w  w w  . ja  v  a  2s .  c om
 * @param id int - ID do usurio
 * @throws UsuarioNaoEncontradoException - usurio no encontrado
 */
@Override
public void removeUsuario(final int id) throws UsuarioNaoEncontradoException {
    EntityManagerFactory emf = Persistence.createEntityManagerFactory("UsuarioPU");
    EntityManager em = emf.createEntityManager();

    Usuario u = em.find(UsuarioImpl.class, id);

    em.getTransaction().begin();
    if (u == null) {
        throw new UsuarioNaoEncontradoException("usuario no encontrado");
    } else {
        em.remove(u);
        em.getTransaction().commit();
    }
    em.clear();
    em.close();
    emf.close();

}

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

@Override
public void deleteAlbum(final Album album) throws AlbumNotFoundException {
    Assert.notNull(album, "No Album supplied !");
    Assert.notNull(album.getId(), "Id should be set for delete !");

    new TxCallback(this.getEmf()) {

        @Override/*from w ww.j a  v  a  2s. com*/
        protected void executeInTransaction(final EntityManager em) {
            final Album managedAlbum = em.find(Album.class, album.getId(), LockModeType.WRITE);
            if (managedAlbum == null) {
                throw new PictureNotFoundException();
            }

            em.remove(managedAlbum);
        }
    };
}

From source file:org.viafirma.persistencia.dao.GenericEjb3Dao.java

/**
 * Elimina la entidad indicada//from w w  w.  ja v  a 2s.  c  om
 * @throws ExcepcionNoCreado 
 */
public void delete(ID id) throws ExcepcionNoCreado {
    log.finest("Eliminando " + persistentClass + " con identificador " + id);
    EntityManager manager = getEntityManager();
    T entidad = null;
    entidad = manager.find(persistentClass, id);
    if (entidad == null) {
        throw new ExcepcionNoCreado("No se puede eliminar " + id);
    }

    try {

        manager.getTransaction().begin();
        manager.remove(entidad);
        manager.getTransaction().commit();
    } catch (Exception e) {
        log.severe("No se puede eliminar " + entidad + " " + e.getMessage());
        manager.getTransaction().rollback();
    } finally {
        manager.close();
    }
}

From source file:edu.csueb.cs6320.utils.UserService.java

public boolean delete(long userid) {
    EntityManager em = Persistence.createEntityManagerFactory("TestPU").createEntityManager();

    User u = em.find(User.class, userid);
    if (u == null) {
        return false;
    }//from w w  w. j  av  a  2s.  c o m
    em.getTransaction().begin();
    em.remove(u);
    em.getTransaction().commit();
    em.close();

    return true;
}

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

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

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

/**
 * Before test.//from   ww w. j a  v  a 2 s .  c o  m
 *
 * @throws Exception the exception
 */
@Before
public void beforeTest() throws Exception {
    EntityManager em = emf.createEntityManager();
    try {
        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);
        }
    } finally {
        em.close();
    }
}