【问题标题】:Simple delete from database从数据库中简单删除
【发布时间】: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


【解决方案1】:

似乎您在约会表中的外键具有 On delete: Restrict 选项。将 Constraintointments_user_id_foreign 更改为 On delete: Cascade,您应该能够在保留外键的同时删除用户。

你可以通过两种方式做到这一点:

一个。首先使用单独的删除语句从约会表中删除相关记录。

b. 添加删除级联选项到约会_user_id_foreign 外键。当您删除用户的记录时,此选项将自动从约会表中删除要删除的用户的所有关联记录。

根据需要试试这个

Query q = session.createQuery("from Stock where stockCode = :stockCode ");
q.setParameter("stockCode", "4715");
Stock stock = (Stock)q.list().get(0);
session.delete(stock);

  @OneToMany(cascade=CascadeType.REMOVE) 
    List<TestFlow> flow = getFlow(flowName);

   //delete the current one
    for(TestFlow tf : flow) {
        Database.deleteObject(tf);
    }

【讨论】:

    【解决方案2】:

    默认情况下,您不能删除父记录并允许子记录作为 ophans。

    在外键约束上添加删除级联以防止出现 ophans。

    ALTER TABLE mydb.testCases DROP FOREIGN KEY fk03;
    ALTER TABLE mydb.testCases ADD CONSTRAINT fk03 FOREIGN KEY (type) REFERENCES 
    testType(idtestType) ON DELETE CASCADE ON UPDATE NO ACTION;
    

    请参阅Hibernate – Cascade example (save, update, delete and delete-orphan) 了解更多信息。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-06-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-01-25
      相关资源
      最近更新 更多