EntitlementCurator extends AbstractHibernateCurator<Entitlement> { @Transactional public void delete(Entitlement entity) { Entitlement toDelete = this.get(entity.getId()); if (toDelete != null) { this.deleteImpl(toDelete); entity.setCertificates(null); entity.getConsumer().getEntitlements().remove(entity); entity.getPool().getEntitlements().remove(entity); } } @Inject  EntitlementCurator(OwnerProductCurator ownerProductCurator, ProductCurator productCurator,
        ConsumerTypeCurator consumerTypeCurator, CandlepinQueryFactory cpQueryFactory); @Transactional Set<Entitlement> bulkUpdate(Set<Entitlement> entitlements); List<Entitlement> listByConsumer(Consumer consumer); List<Entitlement> listByConsumer(Consumer consumer, EntitlementFilterBuilder filters); List<Entitlement> listByConsumerAndPoolId(Consumer consumer, String poolId); Page<List<Entitlement>> listByConsumer(Consumer consumer, String productId,
        EntitlementFilterBuilder filters, PageRequest pageRequest); Page<List<Entitlement>> listByOwner(Owner owner, String productId,
        EntitlementFilterBuilder filters, PageRequest pageRequest); Page<List<Entitlement>> listAll(EntitlementFilterBuilder filters, PageRequest pageRequest); CandlepinQuery<Entitlement> listByOwner(Owner owner); CandlepinQuery<Entitlement> listByEnvironment(Environment environment); CandlepinQuery<Entitlement> listByEnvironment(String environmentId); CandlepinQuery<Entitlement> listByConsumerAndDate(Consumer consumer, Date activeOn); List<Entitlement> listDirty(Consumer consumer); Set<String> listEntitledProductIds(Consumer consumer, Pool pool); int markDependentEntitlementsDirty(Iterable<String> entitlementIds); Set<String> filterDistributorEntitlementIds(Iterable<String> entsIdsToFilter); @Transactional int markEntitlementsDirty(Iterable<String> entitlementIds); @Deprecated Map<Consumer, List<Entitlement>> getDistinctConsumers(List<Entitlement> entsToRevoke); @Transactional void delete(Entitlement entity); void batchDelete(Collection<Entitlement> entitlements); int batchDeleteByIds(Collection<String> entitlementIds); void unlinkEntitlements(Collection<Entitlement> entitlements); @Transactional Entitlement findByCertificateSerial(Long serial); @Transactional Entitlement replicate(Entitlement ent); @SuppressWarnings("unchecked") CandlepinQuery<Entitlement> findByStackId(Consumer consumer, String stackId); @SuppressWarnings("unchecked") CandlepinQuery<Entitlement> findByStackIds(Consumer consumer, Collection stackIds); @SuppressWarnings("unchecked") CandlepinQuery<Entitlement> findByPoolAttribute(Consumer consumer, String attributeName,
        String value); CandlepinQuery<Entitlement> findByPoolAttribute(String attributeName, String value); Entitlement findUpstreamEntitlementForStack(Consumer consumer, String stackId); Collection<String> getDependentEntitlementIdsForPools(Consumer consumer,
        Iterable<String> poolIds);  }
@Test public void removeConsumerWithEntitlements() { beginTransaction(); Consumer c = createConsumer(owner); consumerCurator.create(c); Product product = TestUtil.createProduct(); productCurator.create(product); Pool p = createPool(owner, product, 2L, dateSource.currentDate(), createDate(2020, 1, 1)); poolCurator.create(p); EntitlementCertificate cert = createEntitlementCertificate("keyx", "certificatex"); Entitlement entitlement = createEntitlement(owner, c, p, cert); entitlementCurator.create(entitlement); commitTransaction(); this.getEntityManager().clear(); beginTransaction(); c = consumerCurator.get(c.getId()); c.setOwner(owner); for (Entitlement ent : c.getEntitlements()) { ent.getPool().getEntitlements().remove(ent); Hibernate.initialize(ent.getPool().getEntitlements()); } try { consumerCurator.delete(c); consumerCurator.flush(); } catch (Exception ex) { assertEquals(ex.getCause().getCause().getClass(), SQLIntegrityConstraintViolationException.class); } finally { rollbackTransaction(); } }