EnvironmentCurator extends AbstractHibernateCurator<Environment> { public int deleteEnvironmentsForOwner(Owner owner) { String jpql = "SELECT e.id FROM Environment e WHERE e.owner.id = :owner_id"; List<String> ids = this.getEntityManager() .createQuery(jpql, String.class) .setParameter("owner_id", owner.getId()) .getResultList(); int count = 0; if (ids != null && !ids.isEmpty()) { Map<String, Object> criteria = new HashMap<>(); criteria.put("environment_id", owner.getId()); count = this.bulkSQLDelete(EnvironmentContent.DB_TABLE, criteria); log.info("{} environment-content relations updated", count); criteria.clear(); criteria.put("id", ids); count = this.bulkSQLDelete(Environment.DB_TABLE, criteria); log.info("{} environments deleted", count); } else { log.info("0 environments deleted"); } return count; } EnvironmentCurator(); Environment getConsumerEnvironment(Consumer consumer); CandlepinQuery<Consumer> getEnvironmentConsumers(Environment environment); CandlepinQuery<Consumer> getEnvironmentConsumers(String environmentId); @SuppressWarnings("unchecked") CandlepinQuery<Environment> listForOwner(Owner o); @SuppressWarnings("unchecked") CandlepinQuery<Environment> listForOwnerByName(Owner o, String envName); @SuppressWarnings("unchecked") List<Environment> listWithContent(Set<String> contentIds); int deleteEnvironmentsForOwner(Owner owner); String getEnvironmentIdByName(String ownerId, String envName);  }
@Test public void testDeleteEnvironmentForOwner() { Owner owner1 = this.createOwner("owner1"); Owner owner2 = this.createOwner("owner2"); Content content1 = this.createContent("c1", "c1", owner1); Content content2 = this.createContent("c2", "c2", owner1); Content content3 = this.createContent("c3", "c3", owner2); Environment environment1 = this.createEnvironment( owner1, "test_env-1", "test_env-1", null, null, Arrays.asList(content1) ); Environment environment2 = this.createEnvironment( owner1, "test_env-2", "test_env-2", null, null, Arrays.asList(content2) ); Environment environment3 = this.createEnvironment( owner2, "test_env-3", "test_env-3", null, null, Arrays.asList(content3) ); int output = this.environmentCurator.deleteEnvironmentsForOwner(owner1); assertEquals(2, output); this.environmentCurator.evict(environment1); this.environmentCurator.evict(environment2); this.environmentCurator.evict(environment3); environment1 = this.environmentCurator.get(environment1.getId()); environment2 = this.environmentCurator.get(environment2.getId()); environment3 = this.environmentCurator.get(environment3.getId()); assertNull(environment1); assertNull(environment2); assertNotNull(environment3); assertEquals(1, environment3.getEnvironmentContent().size()); assertEquals(content3.getUuid(), environment3.getEnvironmentContent().iterator().next().getContent() .getUuid()); }