delete AbstractEntity from EntityManager - Java javax.persistence

Java examples for javax.persistence:EntityManager

Description

delete AbstractEntity from EntityManager

Demo Code


import java.io.Serializable;
import javax.persistence.EntityExistsException;
import javax.persistence.EntityManager;

public class Main{
    public static void delete(AbstractEntity entity, EntityManager em) {
        if (entity == null)
            return;
        if (em == null)
            throw new IllegalArgumentException();
        if (!em.isOpen())
            throw new IllegalArgumentException();
        try {//from   w w  w .j a v  a2  s .c o m
            //Mark entity as inactive. Relations are kept.
            entity.setActive(false);
            em.getTransaction().begin();
            try {
                em.persist(entity);
            } catch (EntityExistsException e) {
            }
            em.getTransaction().commit();
        } catch (Exception e) {
            throw e;
        } finally {
            em.getTransaction().rollback();
            em.close();
        }
    }
}

Related Tutorials