HSQLDB数据库中的神秘完整性约束违规

时间:2016-03-03 08:18:11

标签: java sql sql-server hibernate hsqldb

我们正在设计我们的Java应用程序以支持HSQL数据库,以及SQL Server和Oracle等企业数据库。我的任务是编写一个全面的测试套件,它将涵盖应用程序中的主要数据库操作。测试套件对SQL Server运行成功,但是当我使用HSQLDB进行相同的测试时,我一直都会失败。这是堆栈跟踪的一部分:

Caused by: java.sql.SQLIntegrityConstraintViolationException: integrity constraint
violation: foreign key no parent; FKREQKDHUAI8BYVMWC5RMTJ3UFA table: FAVORITE_FILE
    at org.hsqldb.jdbc.JDBCUtil.sqlException(Unknown Source)
    at org.hsqldb.jdbc.JDBCUtil.sqlException(Unknown Source)
    at org.hsqldb.jdbc.JDBCPreparedStatement.fetchResult(Unknown Source)
    at org.hsqldb.jdbc.JDBCPreparedStatement.executeUpdate(Unknown Source)
    at com.mchange.v2.c3p0.impl.NewProxyPreparedStatement.executeUpdate(NewProxyPreparedStatement.java:384)
    at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.executeUpdate(ResultSetReturnImpl.java:204)
    ... 91 more
Caused by: org.hsqldb.HsqlException: integrity constraint violation: foreign key no parent;
FKREQKDHUAI8BYVMWC5RMTJ3UFA table: FAVORITE_FILE
    at org.hsqldb.error.Error.error(Unknown Source)
    at org.hsqldb.Constraint.getException(Unknown Source)
    at org.hsqldb.Constraint.checkInsert(Unknown Source)
    at org.hsqldb.StatementDML.performIntegrityChecks(Unknown Source)
    at org.hsqldb.StatementDML.insertSingleRow(Unknown Source)
    at org.hsqldb.StatementInsert.getResult(Unknown Source)
    at org.hsqldb.StatementDMQL.execute(Unknown Source)
    at org.hsqldb.Session.executeCompiledStatement(Unknown Source)
    at org.hsqldb.Session.execute(Unknown Source)
    ... 95 more

这里真正含糊不清的是,失败发生在测试中与FAVORITE_FILE表无关的地方。相反,我发现在尝试在另一个表(INSERT)上执行OFFLINE_FILE或尝试从另一个表中删除记录时发生此故障。

我希望专家能够看到这个堆栈跟踪和用例,并且可以提供一些有关正在发生的事情的见解。重申一下,在SQL Server上运行时不会出现这些错误。我的预感是HSQLDB的持久层有一些问题/错误,但我无法证实这一点。

1 个答案:

答案 0 :(得分:4)

经过更广泛的测试后,我们发现在SQL Server和Oracle上也发生了类似的问题。事实证明,我们没有关闭/** * @ngdoc function * @name yourModule.yourService * @description * * Your short description * * @constructor * @param {string} yourConstructorParameter A string paramter * @returns {Object} An object instance of type YourClassName * */ angular .module('yourModule', []) .factory('YourClassName', function() { /** * @ngdoc object * @name yourModule.type:YourClassname * @description * * Short description. * */ function YourClassName(yourConstructorParameter) { this.yourConstructorParameter = yourConstructorParameter; this.yourMethod = function() {}; } /** * @ngdoc function * @name yourModule.type:YourClassName#yourPrototypeMethod * @methodOf yourModule.type:YourClassName * @description * * Short Description. * * @returns {Object} The object passed on instantiation. */ YourClassName.prototype.yourPrototypeMethod = function() { return this.yourConstructorParameter; }; return YourClassName; }); 表上的事务。这个Hibernate错误是我们得到的错误消息的根本原因。关闭交易导致错误消失。

这个问题对我们来说是一次很好的学习经历,因为我们意识到在面对Hibernate的错误时,我们应该在质疑数据库之前质疑我们自己代码的正确性。