public void testMetamodelCriteriaDelete() { if ((getPersistenceUnitServerSession()).getPlatform().isSymfoware()) { getPersistenceUnitServerSession() .logMessage( "Test simpleDelete skipped for this platform, " + "Symfoware doesn't support UpdateAll/DeleteAll on multi-table objects (see rfe 298193)."); return; } EntityManager em = createEntityManager(); try { beginTransaction(em); int nrOfEmps = ((Number) em.createQuery( "SELECT COUNT(phone) FROM PhoneNumber phone where phone.owner.firstName is not null") .getSingleResult()) .intValue(); Metamodel metamodel = em.getMetamodel(); EntityType<PhoneNumber> entityPhone_ = metamodel.entity(PhoneNumber.class); EntityType<Employee> entityEmp_ = metamodel.entity(Employee.class); // test query "Delete Employee e where e.firstName is not null"; CriteriaBuilder qb = em.getCriteriaBuilder(); CriteriaDelete<PhoneNumber> cq = qb.createCriteriaDelete(PhoneNumber.class); Root<PhoneNumber> root = cq.from(entityPhone_); cq.where( qb.isNotNull( root.get(entityPhone_.getSingularAttribute("owner", Employee.class)) .get(entityEmp_.getSingularAttribute("firstName")))); Query testQuery = em.createQuery(cq); int updated = testQuery.executeUpdate(); assertEquals( "testCriteriaDelete: wrong number of deleted instances" + updated, nrOfEmps, updated); // check database changes int nr = ((Number) em.createQuery( "SELECT COUNT(phone) FROM PhoneNumber phone where phone.owner.firstName is not null") .getSingleResult()) .intValue(); assertEquals("testCriteriaDelete: found " + nr + " PhoneNumbers after delete all", 0, nr); } finally { if (isTransactionActive(em)) { rollbackTransaction(em); } closeEntityManager(em); } }
///// DELETE Criteria tests: public void simpleMetamodelCriteriaDeleteTest() { if ((getPersistenceUnitServerSession()).getPlatform().isSymfoware()) { getPersistenceUnitServerSession() .logMessage( "Test simpleDelete skipped for this platform, " + "Symfoware doesn't support UpdateAll/DeleteAll on multi-table objects (see rfe 298193)."); return; } EntityManager em = createEntityManager(); int nrOfEmps = ((Number) em.createQuery("SELECT COUNT(phone) FROM PhoneNumber phone").getSingleResult()) .intValue(); Metamodel metamodel = em.getMetamodel(); EntityType<PhoneNumber> entityPhone_ = metamodel.entity(PhoneNumber.class); // test query "Delete PhoneNumber phone"; CriteriaBuilder qb = em.getCriteriaBuilder(); CriteriaDelete<PhoneNumber> cq = qb.createCriteriaDelete(PhoneNumber.class); Root<PhoneNumber> root = cq.from(entityPhone_); beginTransaction(em); try { Query q = em.createQuery(cq); int updated = q.executeUpdate(); assertEquals( "simpleCriteriaDeleteTest: wrong number of deleted instances" + updated, nrOfEmps, updated); // check database changes int nr = ((Number) em.createQuery("SELECT COUNT(phone) FROM PhoneNumber phone").getSingleResult()) .intValue(); assertEquals("simpleCriteriaDeleteTest: found " + nr + " employees after delete all", 0, nr); } finally { if (isTransactionActive(em)) { rollbackTransaction(em); } closeEntityManager(em); } }