title: Can't delete a JPA entity that is part of a @ManyToOne relationship
body: <p>I have two domain objects like so:</p>
  
  <pre><code>@Entity
  public class Employee {
    @Id
    @Column(nullable = false, name = "id")
    protected Integer id;
  
    // Note: org_id is just an integer column in the database
    @JoinColumn(nullable = true, name = "org_id")
    @ManyToOne(targetEntity = Org.class)
    private Org org;
  }
  </code></pre>
  
  <p>...and:</p>
  
  <pre><code>@Entity
  public class Org {
    @Id
    @Column(nullable = false, name = "id")
    protected Integer id;
  }
  </code></pre>
  
  <p>I've come to the situation in my logic where I need to make some drastic changes to what's actually saved in the database. i.e. some Orgs are getting deleted and the Employees who were in them are getting re-allocated.</p>
  
  <p>The issue I have is that my program logic currently does the following:</p>
  
  <ol>
  <li>Delete any Employees that need to be deleted via <code>org.springframework.data.repository.delete(Iterable&lt;? extends T&gt; itrbl)</code></li>
  <li>Delete any Orgs that need to be deleted via <code>org.springframework.data.repository.delete(Iterable&lt;? extends T&gt; itrbl)</code></li>
  <li>Create new/update existing Orgs via <code>org.springframework.data.repository.save(Iterable&lt;S&gt; itrbl)</code></li>
  <li>Create new/update existing Employees via <code>org.springframework.data.repository.save(Iterable&lt;S&gt; itrbl)</code></li>
  </ol>
  
  <p>The issue comes about at step 2. I get an exception like this:</p>
  
  <blockquote>
    <p>org.springframework.dao.InvalidDataAccessApiUsageException:
    org.hibernate.TransientPropertyValueException: object references an
    unsaved transient instance - save the transient instance before
    flushing : com.sample.domain.Employee.org -> com.sample.domain.Org;
    nested exception is java.lang.IllegalStateException:
    org.hibernate.TransientPropertyValueException: object references an
    unsaved transient instance - save the transient instance before
    flushing : com.sample.domain.Employee.org
    -> com.sample.domain.Org</p>
  </blockquote>
  
  <p>If an Org ends up with no employees I don't want to delete the Org. Likewise, if an Employee of an Org gets deleted I don't want the Org to be deleted either.</p>
  
  <p>I essentially just want something that's the same as how I've got the foreign key setup in PostgreSQL on the <code>employees</code> table:</p>
  
  <pre><code>  CONSTRAINT fk_employees_org_id FOREIGN KEY (org_id)
        REFERENCES public.orgs (id) MATCH SIMPLE
        ON UPDATE NO ACTION ON DELETE SET NULL
  </code></pre>
  
  <p>I've looked at the cascade options, and I'm not sure it's applicable seeing as it's not a straight parent/child relationship (and the <code>Employee</code> that defines the <code>@ManyToOne</code> relationship isn't really the parent - it's the child) and it's not bi-directional (there's no need for an Org to have a list of all of its Employees)</p>