PersistenceServiceImpl implements PersistenceProvider { @Override public void delete(Collection<AbstractComponent> componentsToDelete) { EntityManager em = entityManagerFactory.createEntityManager(); try { em.getTransaction().begin(); for (AbstractComponent component:componentsToDelete) { ComponentSpec componentToDelete = em.find(ComponentSpec.class, component.getComponentId()); if (componentToDelete == null) { continue; } TypedQuery<ComponentSpec> q = em.createNamedQuery("ComponentSpec.findReferencingComponents", ComponentSpec.class); q.setParameter("component", component.getComponentId()); List<ComponentSpec> referencingComponents = q.getResultList(); Date lastModified = lastPollTime != null ? lastPollTime.getAdjustedNow() : getCurrentTimeFromDatabase(); if (lastModified == null) { lastModified = new Date(); } for (ComponentSpec cs:referencingComponents) { cs.getReferencedComponents().remove(componentToDelete); cs.setLastModified(lastModified != null ? lastModified : new Date()); } em.remove(componentToDelete); } em.getTransaction().commit(); } finally { if (em.getTransaction().isActive()) { em.getTransaction().rollback(); } em.close(); } }  void bind(Platform platform); void unbind(Platform platform); void setEntityManagerProperties(Properties p); void activate(ComponentContext context); @Override AbstractComponent getComponentFromStore(String componentId); @Override AbstractComponent getComponent(String componentId); @Override Collection<AbstractComponent> getReferences(AbstractComponent component); @Override void startRelatedOperations(); @Override void completeRelatedOperations(boolean save); @Override void addComponentToWorkUnit(AbstractComponent component); Collection<AbstractComponent> getCurrentComponents(); @Override Set<String> getAllUsers(); @Override Collection<String> getUsersInGroup(String group); @Override void persist(Collection<AbstractComponent> componentsToPersist); @Override void delete(Collection<AbstractComponent> componentsToDelete); @Override boolean hasComponentsTaggedBy(String tagId); @Override List<AbstractComponent> getReferencedComponents(
			AbstractComponent component); @Override User getUser(String userId); @SuppressWarnings("unchecked") QueryResult findComponentsByBaseDisplayedNamePattern(String pattern, Properties props); @Override T getComponent(String externalKey, Class<T> componentType); @Override void updateComponentsFromDatabase(); @Override List<AbstractComponent> getBootstrapComponents(); @Override void addNewUser(String userId, String groupId, AbstractComponent mysandbox, AbstractComponent dropbox); @Override Map<String, ExtendedProperties> getAllProperties(String componentId); @Override AbstractComponent getComponent(String externalKey,
			String componentType); @Override void tagComponents(String tag,
			Collection<AbstractComponent> components);  }
@Test public void testDelete() { em.getTransaction().begin(); final String childId = "3"; Tag t3 = new Tag(); t3.setTagId("t3"); em.persist(t3); ComponentSpec child = createComponentSpec(childId, "xyz", "deleted", "123", "xyz", Collections.<Tag>emptyList(), Collections.<String,String>emptyMap()); ComponentSpec parent1 = createComponentSpec("1", "xyz", "parent1", "123", "xyz", Collections.<Tag>emptyList(), Collections.<String,String>emptyMap()); ComponentSpec parent2 = createComponentSpec("2", "xyz", "parent2", "123", "xyz", Collections.<Tag>emptyList(), Collections.<String,String>emptyMap()); ComponentSpec parent3 = createComponentSpec("p2", "xyz", "parent2", "123", "xyz", Collections.<Tag>singletonList(t3), Collections.<String,String>emptyMap()); List<ComponentSpec> parents = Arrays.asList(parent1,parent2); for (ComponentSpec cs : parents) { cs.getReferencedComponents().add(child); } parent3.getReferencedComponents().add(child); em.persist(child); em.persist(parent1); em.persist(parent2); em.persist(parent3); em.getTransaction().commit(); em.clear(); int objVersion = em.find(ComponentSpec.class, "1").getObjVersion(); em.clear(); Assert.assertNotNull(em.find(Tag.class, "t3")); AbstractComponent parent2Component = serviceImpl.getComponent("p2"); serviceImpl.delete(Collections.singleton(parent2Component)); Assert.assertNotNull(em.find(Tag.class, "t3")); AbstractComponent deleted = serviceImpl.getComponent(child.getComponentId()); serviceImpl.delete(Collections.singleton(deleted)); Assert.assertNull(em.find(ComponentSpec.class, childId)); for (ComponentSpec cs: parents) { ComponentSpec p = em.find(ComponentSpec.class, cs.getComponentId()); Assert.assertEquals(p.getObjVersion(), objVersion+1); } AbstractComponent unsavedObj = Mockito.mock(AbstractComponent.class); Mockito.when(unsavedObj.getComponentId()).thenReturn("thisObjUnsaved"); serviceImpl.delete(Collections.singleton(unsavedObj)); }