Hibernate/H2 @OneToMany "Referential integrity constraint violation" 删除 child?
Hibernate/H2 @OneToMany "Referential integrity constraint violation" on remove of child?
你好,我的关系很简单。
一个患者可以有多个测量值。我使用了 H2 文件数据库和 Hibernate。
问题:
当我尝试从患者身上移除测量值时,出现以下错误。
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]
关系的患者部分:
@OneToMany(orphanRemoval=true, cascade={CascadeType.PERSIST, CascadeType.REMOVE, CascadeType.MERGE}, mappedBy = "patient", fetch = FetchType.EAGER)
public List<MeasurementVO> getMeasurements() {
return measurements;
}
关系的测量部分:
@ManyToOne
@JoinColumn
public PatientVO getPatient() {
return patient.get();
}
应该删除测量的函数:
@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;
}
}
持久性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>
有人提示我为什么无法从患者身上移除测量值然后坚持 him/her 吗?
从约束被违反的 table (PATIENTVO_MEASUREMENTVO
) 的名称来看,您似乎有一个中间 table 来实现关系。这通常用于多对多关系。
作为一对多关系,为什么不直接从 Measurement 添加外键到 Patient?
你好,我的关系很简单。 一个患者可以有多个测量值。我使用了 H2 文件数据库和 Hibernate。
问题: 当我尝试从患者身上移除测量值时,出现以下错误。
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]
关系的患者部分:
@OneToMany(orphanRemoval=true, cascade={CascadeType.PERSIST, CascadeType.REMOVE, CascadeType.MERGE}, mappedBy = "patient", fetch = FetchType.EAGER)
public List<MeasurementVO> getMeasurements() {
return measurements;
}
关系的测量部分:
@ManyToOne
@JoinColumn
public PatientVO getPatient() {
return patient.get();
}
应该删除测量的函数:
@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;
}
}
持久性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>
有人提示我为什么无法从患者身上移除测量值然后坚持 him/her 吗?
从约束被违反的 table (PATIENTVO_MEASUREMENTVO
) 的名称来看,您似乎有一个中间 table 来实现关系。这通常用于多对多关系。
作为一对多关系,为什么不直接从 Measurement 添加外键到 Patient?