【发布时间】:2018-02-26 11:17:09
【问题描述】:
我正在尝试使用通用方法从数据库中删除数据。它适用于添加、更新,但在删除时我得到了这个:
java.sql.SQLIntegrityConstraintViolationException:无法删除或 更新父行:外键约束失败 (
mydb.testCases, 约束fk03外键 (type) 参考testType(idtestType) ON DELETE NO ACTION ON UPDATE NO ACTION)
数据库
//delete the object to the database
public static void deleteObject(Object object) {
SessionFactory factory = HibernateUtil.GetSessionFactory();
Session session = factory.openSession();
Transaction tx = null;
try{
tx = session.beginTransaction();
session.delete(object);
tx.commit();
}catch (HibernateException e) {
if (tx!=null) tx.rollback();
e.printStackTrace();
}finally {
session.close();
}
}
我的删除尝试
List<TestFlow> flow = getFlow(flowName);
//delete the current one
for(TestFlow tf : flow) {
Database.deleteObject(tf);
}
HBM
<hibernate-mapping>
<class name="com.atp.Model.TestCases.TestFlow" table="testFlow">
<meta attribute="class-description">
This class contains the testCases flow details.
</meta>
<id name="id" type="int" column="idtestFlow">
<generator class="native"/>
</id>
<property name="name" column="name" type="string"/>
<many-to-one name="testCase" class="com.atp.Model.TestCases.TestCase" column="testCase" fetch="select" cascade="all" lazy="false"/>
<property name="rowNumber" column="rowNumber" type="int"/>
<property name="testCaseStatus" column="testCaseStatus" type="int"/>
<property name="params" column="params" type="string" />
<property name="creationDate" column="creationDate" type="string"/>
<property name="createdBy" column="createdBy" type="string"/>
<property name="targetType" column="targetType" type="string"/>
<property name="targetName" column="targetName" type="string"/>
<property name="output" column="output" type="string"/>
<property name="completionDate" column="completionDate" type="string"/>
<property name="isCompleted" column="isCompleted" type="int"/>
</class>
【问题讨论】:
-
这可能不是一个真正的 Java 问题,只是一个 MySQL 问题,最有可能的问题是您正在尝试删除一条仍被其他表中的记录引用的记录(这基本上是错误信息是什么意思,用简单的英语)。
标签: java mysql database hibernate