简体   繁体   中英

Hibernate/H2 @OneToMany “Referential integrity constraint violation” on remove of child?

Hello I have a rather simple relationship. A Patient can have several Measurements. I used a H2 file database and Hibernate.

The Problem: When I try to remove a Measurement from a Patient I get a the following Error.

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]

The Patient part of the Relationship:

@OneToMany(orphanRemoval=true, cascade={CascadeType.PERSIST, CascadeType.REMOVE, CascadeType.MERGE}, mappedBy = "patient", fetch = FetchType.EAGER)
public List<MeasurementVO> getMeasurements() {
    return measurements;
}

The Measurement Part of the Relationship:

@ManyToOne
@JoinColumn
public PatientVO getPatient() {
    return patient.get();
}

The function which should delete a Measurement:

@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 -> measurement.getId().equals(measurementVO.getId()));
            manager.merge(patientAlreadyInDB);
            return true;
        } else {
            return false;
        }
    }

And the Persistence XML:

<?xml version="1.0" encoding="UTF-8" ?>
<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">

    <persistence-unit name="db" transaction-type="RESOURCE_LOCAL">
        <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>

        <class>ch.fhnw.ima.doggait.vo.PatientVO</class>
        <class>ch.fhnw.ima.doggait.vo.MeasurementVO</class>
        <class>ch.fhnw.ima.doggait.vo.MeasurementAttributesVO</class>
        <class>ch.fhnw.ima.doggait.vo.TimeMark</class>

        <properties>
            <property name="connection.driver_class" value="org.h2.Driver"/>
           <!-- <property name="hibernate.connection.url" value="jdbc:h2:mem:db"/>-->
            <property name="hibernate.connection.url" value="jdbc:h2:file:./database/dogdatabase"/>
            <property name="hibernate.dialect" value="org.hibernate.dialect.H2Dialect"/>
            <!--<property name="hibernate.hbm2ddl.auto" value="create-drop"/>-->
            <property name="hibernate.hbm2ddl.auto" value="update"/>
            <property name="hibernate.show_sql" value="true"/>
        </properties>
    </persistence-unit>
</persistence>

Does somebody has a hint why I am not able to remove a Measurement from a patient and then persist him/her?

From the name of the table whose constraint is being violated ( PATIENTVO_MEASUREMENTVO ), seems like you have an intermediate table to implement the relation. This is normally used in many to many relationships.

Being a one to many relationship why not just adding a foreign key from the Measurement to Patient?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM