title: Hibernate delete child when parent is getting updated in same transaction
body: <p>I have defined two entities Customer and SalesOrder in my domain model with oneToMany relationship from customer to salesOrder.
  Below code deletes the sales order: </p>
  
  <pre><code>SalesOrder salesOrder = salesOrderRepository.findByOrderNumber(orderNumber);
  if(null != salesOrder){
          Customer customer = salesOrder.getCustomer();
          customer.setCurrentCredit(customer.getCurrentCredit() - salesOrder.getTotalPrice());
  Iterator&lt;SalesOrder&gt; iter = customer.getSalesOrders().iterator();
          while(iter.hasNext()){
              SalesOrder order = iter.next();
              if(order.getId().equals(salesOrder.getId())){
                  iter.remove();
                  break;
              }
          }
          customerRepository.save(customer);
  </code></pre>
  
  <p>But the below code does not delete the sales order.</p>
  
  <pre><code>SalesOrder salesOrder = salesOrderRepository.findByOrderNumber(orderNumber);
  if(null != salesOrder){
          Customer customer = salesOrder.getCustomer();
          customer.setCurrentCredit(customer.getCurrentCredit() - salesOrder.getTotalPrice());
  SalesOrder salesOrder = salesOrderRepository.findByOrderNumber(orderNumber);
  if(null != salesOrder){
          Customer customer = salesOrder.getCustomer();
          customer.setCurrentCredit(customer.getCurrentCredit() - salesOrder.getTotalPrice());
          salesOrderRepository.delete(salesOrder);
          customerRepository.save(customer);
  </code></pre>
  
  <p>Below are the domain classes : 
  Customer.java</p>
  
  <pre><code>@Entity
  @Table(name = "customer", uniqueConstraints = {       @UniqueConstraint(columnNames = { "code" }) })
  public class Customer extends BaseEntity {
  
  @Column
  private String code;
  
  @Column
  private String name;
  
  @Column
  private String address;
  
  @Column
  private String phone1;
  
  @Column
  private String phone2;
  
  @Column
  private Double creditLimit;
  
  @Column
  private Double currentCredit;
  
  @OneToMany(cascade=CascadeType.ALL,mappedBy="customer",fetch=FetchType.LAZY,orphanRemoval = true)
  @LazyCollection(LazyCollectionOption.FALSE)
  private List&lt;SalesOrder&gt; salesOrders = new ArrayList&lt;SalesOrder&gt;();
  
  /**
   * @return the code
   */
  public String getCode() {
      return code;
  }
  
  /**
   * @param code the code to set
   */
  public void setCode(String code) {
      this.code = code;
  }
  
  /**
   * @return the name
   */
  public String getName() {
      return name;
  }
  
  /**
   * @param name the name to set
   */
  public void setName(String name) {
      this.name = name;
  }
  
  /**
   * @return the address
   */
  public String getAddress() {
      return address;
  }
  
  /**
   * @param address the address to set
   */
  public void setAddress(String address) {
      this.address = address;
  }
  
  /**
   * @return the phone1
   */
  public String getPhone1() {
      return phone1;
  }
  
  /**
   * @param phone1 the phone1 to set
   */
  public void setPhone1(String phone1) {
      this.phone1 = phone1;
  }
  
  /**
   * @return the phone2
   */
  public String getPhone2() {
      return phone2;
  }
  
  /**
   * @param phone2 the phone2 to set
   */
  public void setPhone2(String phone2) {
      this.phone2 = phone2;
  }
  
  /**
   * @return the creditLimit
   */
  public Double getCreditLimit() {
      return creditLimit;
  }
  
  /**
   * @param creditLimit the creditLimit to set
   */
  public void setCreditLimit(Double creditLimit) {
      this.creditLimit = creditLimit;
  }
  
  /**
   * @return the currentCredit
   */
  public Double getCurrentCredit() {
      return currentCredit;
  }
  
  /**
   * @param currentCredit the currentCredit to set
   */
  public void setCurrentCredit(Double currentCredit) {
      this.currentCredit = currentCredit;
  }
  
  /**
   * @return the salesOrders
   */
  public List&lt;SalesOrder&gt; getSalesOrders() {
      return salesOrders;
  }
  
  /**
   * @param salesOrders the salesOrders to set
   */
  public void setSalesOrders(List&lt;SalesOrder&gt; salesOrders) {
      this.salesOrders = salesOrders;
  }
  
  
  
  }
  </code></pre>
  
  <p>SalesOrder.java</p>
  
  <pre><code>@Entity
  @Table(name = "sales_order", uniqueConstraints = { @UniqueConstraint(columnNames = { "orderNumber" }) })
  public class SalesOrder extends BaseEntity {
  
  @Column
  private  String orderNumber;
  
  @ManyToOne(cascade=CascadeType.DETACH, fetch=FetchType.LAZY)
  @JoinColumn(name="customer_id",referencedColumnName="id")
  private Customer customer;
  
  @Column
  private Double totalPrice;
  
  @OneToMany(cascade=CascadeType.ALL,mappedBy="salesOrder",fetch=FetchType.LAZY,orphanRemoval = true)
  @LazyCollection(LazyCollectionOption.FALSE)
  private List&lt;OrderLines&gt; orderLines= new ArrayList&lt;OrderLines&gt;();
  
  /**
   * @return the orderNumber
   */
  public String getOrderNumber() {
      return orderNumber;
  }
  
  /**
   * @param orderNumber the orderNumber to set
   */
  public void setOrderNumber(String orderNumber) {
      this.orderNumber = orderNumber;
  }
  
  /**
   * @return the customer
   */
  public Customer getCustomer() {
      return customer;
  }
  
  /**
   * @param customer the customer to set
   */
  public void setCustomer(Customer customer) {
      this.customer = customer;
  }
  
  /**
   * @return the totalPrice
   */
  public Double getTotalPrice() {
      return totalPrice;
  }
  
  /**
   * @param totalPrice the totalPrice to set
   */
  public void setTotalPrice(Double totalPrice) {
      this.totalPrice = totalPrice;
  }
  
  /**
   * @return the orderLines
   */
  public List&lt;OrderLines&gt; getOrderLines() {
      return orderLines;
  }
  
  /**
   * @param orderLines the orderLines to set
   */
  public void setOrderLines(List&lt;OrderLines&gt; orderLines) {
      this.orderLines = orderLines;
  }   
  
  }
  </code></pre>