title: Unable to delete parent JPA entity with @OneToMany relationship using Hibernate
body: <p>I've hit a problem when using Hibernate to delete a JPA entity that has a <code>@OneToMany</code> relationship with a child entity, but the same code works fine when using EclipseLink instead of Hibernate as the JPA provider. The annotation on the parent entity is <code>@OneToMany(fetch = FetchType.EAGER, cascade = {CascadeType.ALL}, orphanRemoval=true)</code></p>
  
  <p>When using Hibernate it attempts to set the join column on the child entity to null which fails as the column does not allow nulls. When using EclipseLink it deletes the child entities first and then deletes the parent entity which is the desired behaviour.</p>
  
  <p>My questions are:</p>
  
  <ol>
  <li>Why is the behaviour different between Hibernate and EclipseLink? My understand that the <code>orphanRemoval</code> and <code>CascaseType.REMOVE</code> features in JPA2 should function that same with both providers.</li>
  <li>Is there anything I can change in my code to allow both EclipseLink and Hibernate to function the same when deleting the parent entity, without deleting the child entities first? One constraint is that code must work with both JPA providers.</li>
  </ol>
  
  <p>I've found several similar questions on SO, however most of them either relate to JPA 1.0, using depreciated Hibernate annotations or suggest setting <code>orphanRemoval=true</code> on the <code>@OneToMany</code> annotation.</p>
  
  <p>The version of Hibernate I'm using is 4.3.10.Final and the version of EclipseLink is 2.5.2.</p>
  
  <p>To demonstrate the problem I've created the following relatively simple example.</p>
  
  <p>The code for the parent entity is as follows:</p>
  
  <pre><code>@Entity
  @Table(name="HOUSE")
  public class HouseEntity {
  
      @Id
      @Column(name="HOUSE_ID")
      private int houseId;
  
      @Column(name="HOUSE_NAME")
      private String houseName;
  
      @OneToMany(fetch = FetchType.EAGER, cascade = {CascadeType.ALL}, orphanRemoval=true)
      @JoinColumn(name = "HOUSE_ID", referencedColumnName = "HOUSE_ID")
      private List&lt;RoomEntity&gt; rooms;
  
      //Getters, setters, equals, and hashCode omitted
  }
  </code></pre>
  
  <p>The code for the child entity is as follows:</p>
  
  <pre><code>@Entity
  @Table(name="ROOM")
  public class RoomEntity {
  
      @Id
      @Column(name="ROOM_ID")
      private int roomId;
  
      @ManyToOne(fetch = FetchType.EAGER)
      @JoinColumn(name = "HOUSE_ID", referencedColumnName = "HOUSE_ID")
      private HouseEntity house;
  
      @Column(name="ROOM_NAME")
      private String roomName;
  
      //Getters, setters, equals, and hashCode omitted
  }
  </code></pre>
  
  <p>The EJB code used to delete the parent entity is as follows:</p>
  
  <pre><code>@Stateless
  public class HouseService {
  
      @PersistenceContext
      private EntityManager em;
  
      public void deleteHouse(int houseId) {
          HouseEntity houseEntity = em.find(HouseEntity.class, houseId);
          em.remove(houseEntity);
      }
  }
  </code></pre>
  
  <p>In Hibernate the following SQL statement is executed against the database
  <code>update ROOM set HOUSE_ID=null where HOUSE_ID=?</code> which fails with a <code>ORA-01407: cannot update ("TEST"."ROOM"."HOUSE_ID") to NULL</code></p>
  
  <p>In EclipseLink the SQL statement <code>DELETE FROM ROOM WHERE (HOUSE_ID = ?)</code> is executed first and then the SQL statement <code>DELETE FROM HOUSE WHERE (HOUSE_ID = ?)</code> is executed.</p>