EntityManager em = entityManagerFactory.createEntityManager(); em.getTransaction().begin(); // find the entity to be removed Person p = em.find(Person.class, 1L); // remove the entity em.remove(p); em.getTransaction().commit(); em.close();
EntityManager em = entityManagerFactory.createEntityManager(); em.getTransaction().begin(); // create a CriteriaBuilder CriteriaBuilder cb = em.getCriteriaBuilder(); // create the CriteriaQuery CriteriaQueryBoth examples use the javax.persistence.EntityManager and javax.persistence.EntityManagerFactory packages from Java Persistence API (JPA) library.criteria = cb.createQuery(Person.class); // specify the root entity Root root = criteria.from(Person.class); // specify the criteria criteria.select(root).where(cb.greaterThan(root.get("age"), 30)); // execute the criteria and delete the results List persons = em.createQuery(criteria).getResultList(); for (Person p : persons) { em.remove(p); } em.getTransaction().commit(); em.close();