title: Removing entities from Collection in hibernate
body: <p>I'm trying to manually delete every entity that's in a collection on an entity. The problem is, the entities don't get deleted from the database, even though they get removed from the collection on the task.</p>
  
  <p>Below is the code im using to achieve this:</p>
  
  <pre><code>public int removeExistingCosts(final DataStoreTask task) {
  
      int removedAccumulator = 0;
      Query query = entityManager.createNamedQuery(DataStoreCost.GET_COSTS_FOR_TASK);
      query.setParameter(DataStoreCost.TASK_VARIABLE_NAME, task);
  
  
      try {
  
          List costsForTask = query.getResultList();
          for(Object cost : costsForTask) {
              task.getCosts().remove(cost);
              removedAccumulator++;
          }
  
      } catch (NoResultException e) {
          logger.debug("Couldn't costs for task: {}", task.getId());
      }
  
      entityManager.flush();
      entityManager.persist(task);
  
      return removedAccumulator;
  }
  </code></pre>
  
  <p>Any ideas?</p>
  
  <p>P.S the collection is represented as:</p>
  
  <pre><code>@OneToMany(targetEntity = DataStoreCost.class, mappedBy = "task", cascade = CascadeType.ALL)
  private Collection&lt;DataStoreCost&gt; costs;
  </code></pre>
  
  <p>Cheers.</p>