从数据库中删除一个简单行

时间:2018-05-02 09:02:40

标签: java mysql hibernate

我有2个表,ANDtestInput,而testCases我有一个FK,其ID为另一个表。

所以基本上我要删除的行是输入的id,testCase的id,名称和描述。

testInput

我试图删除该行,然后我

  

java.sql.SQLIntegrityConstraintViolationException:无法删除或   更新父行:外键约束失败   ('43', '21', 'USERNAME', 'USERNAME' '44', '21', 'PASSWORD', 'PASSWORD' mydb,CONSTRAINT testInput外键(fk02)   参考testCasetestCases)ON更新没有动作更新NO   ACTION)

我不想删除testCase的记录。我只想删除该testCase的输入。我该怎么办?

如果你想要

代码

idtestCase

xml TestCases

  List<TestInput> previousInputs = TestInput.getInputs(testCaseName);
        for(TestInput in : previousInputs) {
            Database.deleteObject(in);
        }



 //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();
    }
}

xml testInput

   <hibernate-mapping>
<class name="TestCase" table="testCases">
    <meta attribute="class-description">
        This class contains the testCases details.
    </meta>
    <id name="id" type="int" column="idtestCase">
        <generator class="native"/>
    </id>
    <property name="name" column="name" type="string"/>
    <many-to-one name="type" class="TestType" column="type" fetch="select" lazy="false"/>
    <property name="data" column="data" type="binary"/>
    <property name="amountOfInputs" column="amountOfInputs" type="int"/>
    <property name="creationDate" column="creationDate" type="string"/>
    <property name="createdBy" column="createdBy" type="string"/>
    <many-to-one name="tellerConfig" class="TellerConfiguration" column="tellerConfig" fetch="select" lazy="false"/>
</class>
</hibernate-mapping>

1 个答案:

答案 0 :(得分:0)

将外键fk02上的约束从'NO ACTION'更改为'SET NULL'

FOREIGN KEY (idtestcase)
REFERENCES testCases(idtestCase)
ON DELETE SET NULL