title: Hibernate/H2 @OneToMany "Referential integrity constraint violation" on remove of child?
body: <p>Hello I have a rather simple relationship. 
  A Patient can have several Measurements. I used a H2 file database and Hibernate.</p>
  
  <p>The Problem:
  When I try to remove a Measurement from a Patient I get a the following Error.</p>
  
  <pre><code>Referential integrity constraint violation: "FK_O423U89KR6K78NNG1PO0ISDF6: PUBLIC.PATIENTVO_MEASUREMENTVO FOREIGN KEY(MEASUREMENTS_ID) REFERENCES PUBLIC.MEASUREMENTVO(ID) (X'3c2b9ee868f645c5a5a743c2a409ab5e')"; SQL statement:
  delete from MeasurementVO where id=? [23503-185]
  </code></pre>
  
  <p>The Patient part of the Relationship:</p>
  
  <pre><code>@OneToMany(orphanRemoval=true, cascade={CascadeType.PERSIST, CascadeType.REMOVE, CascadeType.MERGE}, mappedBy = "patient", fetch = FetchType.EAGER)
  public List&lt;MeasurementVO&gt; getMeasurements() {
      return measurements;
  }
  </code></pre>
  
  <p>The Measurement Part of the Relationship:</p>
  
  <pre><code>@ManyToOne
  @JoinColumn
  public PatientVO getPatient() {
      return patient.get();
  }
  </code></pre>
  
  <p>The function which should delete a Measurement:</p>
  
  <pre><code>@Override
  @Transactional
  public boolean removeMeasurementFromPatient(PatientVO patientVO, MeasurementVO measurementVO) {
          EntityManager manager = emProvider.get();
          PatientVO patientAlreadyInDB = manager.find(PatientVO.class, patientVO.getPatientId());
  
          if (patientAlreadyInDB != null) {
              patientAlreadyInDB.getMeasurements().removeIf(measurement -&gt; measurement.getId().equals(measurementVO.getId()));
              manager.merge(patientAlreadyInDB);
              return true;
          } else {
              return false;
          }
      }
  </code></pre>
  
  <p>And the Persistence XML:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8" ?&gt;
  &lt;persistence xmlns="http://java.sun.com/xml/ns/persistence"
               xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
               xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
   http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd" version="1.0"&gt;
  
      &lt;persistence-unit name="db" transaction-type="RESOURCE_LOCAL"&gt;
          &lt;provider&gt;org.hibernate.jpa.HibernatePersistenceProvider&lt;/provider&gt;
  
          &lt;class&gt;ch.fhnw.ima.doggait.vo.PatientVO&lt;/class&gt;
          &lt;class&gt;ch.fhnw.ima.doggait.vo.MeasurementVO&lt;/class&gt;
          &lt;class&gt;ch.fhnw.ima.doggait.vo.MeasurementAttributesVO&lt;/class&gt;
          &lt;class&gt;ch.fhnw.ima.doggait.vo.TimeMark&lt;/class&gt;
  
          &lt;properties&gt;
              &lt;property name="connection.driver_class" value="org.h2.Driver"/&gt;
             &lt;!-- &lt;property name="hibernate.connection.url" value="jdbc:h2:mem:db"/&gt;--&gt;
              &lt;property name="hibernate.connection.url" value="jdbc:h2:file:./database/dogdatabase"/&gt;
              &lt;property name="hibernate.dialect" value="org.hibernate.dialect.H2Dialect"/&gt;
              &lt;!--&lt;property name="hibernate.hbm2ddl.auto" value="create-drop"/&gt;--&gt;
              &lt;property name="hibernate.hbm2ddl.auto" value="update"/&gt;
              &lt;property name="hibernate.show_sql" value="true"/&gt;
          &lt;/properties&gt;
      &lt;/persistence-unit&gt;
  &lt;/persistence&gt;
  </code></pre>
  
  <p>Does somebody has a hint why I am not able to remove a Measurement from a patient and then persist him/her?</p>