【问题标题】:DbUnit + HSQLDB + JPA Entity: integrity constraint violation: unique constraint or index violation; UK_25T4P9GXGVRCC8R3VL93JKAJE table: NEW_TABDbUnit + HSQLDB + JPA Entity:完整性约束违规:唯一约束或索引违规; UK_25T4P9GXGVRCC8R3VL93JKAJE 表:NEW_TAB
【发布时间】:2014-11-29 04:18:13
【问题描述】:

我正在使用 DbUnit 框架对我的 JPA 实体 bean 进行单元测试。我已经从数据库表中生成了实体。并且还将 DB 数据导出到 xml 文件中,DbUnit 在执行测试时可以使用这些文件。

但是对于每个测试,我都会收到以下错误:

Caused by: org.hsqldb.HsqlException: integrity constraint violation: unique constraint or index violation; UK_25T4P9GXGVRCC8R3VL93JKAJE table: NEW_TAB
    at org.hsqldb.error.Error.error(Unknown Source)
    at org.hsqldb.Constraint.getException(Unknown Source)
    at org.hsqldb.index.IndexAVLMemory.insert(Unknown Source)
    at org.hsqldb.persist.RowStoreAVL.indexRow(Unknown Source)
    at org.hsqldb.TransactionManager2PL.addInsertAction(Unknown Source)
    at org.hsqldb.Session.addInsertAction(Unknown Source)
    at org.hsqldb.Table.insertSingleRow(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)

我的测试persistence.xml如下:

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="1.0" 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">
    <persistence-unit name="testPU" transaction-type="RESOURCE_LOCAL">
        <provider>org.hibernate.ejb.HibernatePersistence</provider>
        <class>com.dummy.entity.NewTab</class>
        <properties>
            <property name="javax.persistence.jdbc.driver" value="org.hsqldb.jdbcDriver"/>
            <property name="javax.persistence.jdbc.url" value="jdbc:hsqldb:mem:UNITTEST"/>
            <property name="hibernate.dialect" value="org.hibernate.dialect.HSQLDialect"/>
            <property name="javax.persistence.jdbc.user" value="sa"/>
            <property name="javax.persistence.jdbc.password" value=""/>
            <property name="hibernate.hbm2ddl.auto" value="create-drop"/>
            <property name="hibernate.show_sql" value="true"/>
            <property name="hibernate.format_sql" value="true"/>
        </properties>
    </persistence-unit>
</persistence>

每个测试用例都使用:

try {
    final DatabaseConnection connection = new DatabaseConnection(jdbcConnection);
    //connection.getConnection().prepareStatement("SET DATABASE REFERENTIAL INTEGRITY FALSE").execute();
    final DatabaseConfig config = connection.getConfig();
    // config.setProperty(FEATURE_QUALIFIED_TABLE_NAMES, true);
    config.setProperty(PROPERTY_ESCAPE_PATTERN, "\"?\"");
    config.setProperty(PROPERTY_DATATYPE_FACTORY, new HsqldbDataTypeFactory());

    final List<IDataSet> dataSets = getDataSets();
    if (dataSets != null) {
        for (IDataSet dataSet : dataSets) {
            dbOperation.execute(connection, dataSet);
        }
    }
} catch (Exception e) {
    throw new RuntimeException(e);
}

对于多个实体的每次测试,我都会收到此错误。所以我不知何故觉得我的配置有问题。如果您有任何想法,请告诉我。

【问题讨论】:

  • 所以 NEW_TAB 有一个独特的约束,你尝试做一个 INSERT 并且它违反了这一点。那么为什么不包括实体呢?什么是 UNIQUE 约束?
  • 我不知道为什么这个问题被否决了 :( 似乎 DbUnit 存在一些严重的问题。它不尊重数据库限制。所以问题是合法的!

标签: java jpa hsqldb unique-constraint dbunit


【解决方案1】:

我通过删除每个 JPA 实体字段的 @Column 注释的属性 unique=true 解决了这个问题。不知道为什么 DBUnit 不理解该 JPA 实体有一个复合键。

删除 unique = true 不会影响我,因为我没有在 DB 上执行任何创建/更新/取消事务。我只执行读取操作。

【讨论】:

    猜你喜欢
    • 2022-01-23
    • 2018-01-10
    • 2015-11-03
    • 2013-11-27
    • 2018-06-28
    • 2020-04-22
    • 1970-01-01
    • 2018-01-20
    • 2015-05-07
    相关资源
    最近更新 更多