user613114
user613114

Reputation: 2821

DbUnit + HSQLDB + JPA Entity: integrity constraint violation: unique constraint or index violation; UK_25T4P9GXGVRCC8R3VL93JKAJE table: NEW_TAB

I am unit testing my JPA entity beans using DbUnit framework. I have generated entities from DB tables. And also exported DB data into xml files which DbUnit can use while executing tests.

But for every test I receive error as:

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)

My test persistence.xml is as:

<?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>

Every testcase is executed using:

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

For every test for multiple entities I receive this error. So I somehow feel that something is wrong with my configuration. Please let me know, if you have any idea.

Upvotes: 0

Views: 5803

Answers (1)

user613114
user613114

Reputation: 2821

I resolved this issue by removing attribure unique=true for @Column annotation of every JPA entity field. Not sure why DBUnit did not understand that there is a composite key for that JPA enity.

Removing unique = true, will not affect me as I am not doing any create/update/cancel transactions on DB. I am executing only read operations.

Upvotes: 1

Related Questions