0

I'm writing some Arquillian (with Shrinkwrap) tests, and im currently using the JPA 2.1 create and drop automation with scripts, via persistence.xml:

// src/test/resources-wildfly-remote/test-persistence.xml
// runs fine, configured as resource in pom.xml
<?xml version="1.0" encoding="UTF-8"?>
    <persistence version="2.1" xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
    <persistence-unit name="testPU" transaction-type="JTA">
        <!-- Wildfly JPA specific implementation config -->
        <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
        <jta-data-source>java:/jdbc/Test</jta-data-source>
        <exclude-unlisted-classes>false</exclude-unlisted-classes>
        <properties>
            <property name="hibernate.archive.autodetection" value="class" />
            <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect" />
            <property name="javax.persistence.schema-generation.database.action"
            value="drop-and-create" />
            <property name="javax.persistence.schema-generation.create-source"
            value="script" />
            <property name="javax.persistence.schema-generation.drop-source"
            value="script" />
            <property name="javax.persistence.schema-generation.create-script-source"
            value="META-INF/create-test.sql" />
            <property name="javax.persistence.schema-generation.drop-script-source"
            value="META-INF/drop-test.sql" />
        </properties>
    </persistence-unit>
</persistence>

Arquillian w/ Shrinkwrap snippet:

// src/test/java/com/example/MyTest.java
@Deployment
@TargetsContainer("wildfly")
public static Archive<?> createDeployment() {
    return ShrinkWrap
            .create(WebArchive.class, "test.war")
            .addAsResource("create-test.sql", "META-INF/create-test.sql")
            .addAsResource("drop-test.sql", "META-INF/drop-test.sql")
            .addAsResource("test-persistence.xml",
                    "META-INF/persistence.xml")
            .addAsWebInfResource("example-ds.xml")
            .addAsWebInfResource(EmptyAsset.INSTANCE, "beans.xml");
}

Project's relevant structure:

--project
    --src/test
        --java
            --com/example/MyTest.java
        --resources
            --create-test.sql
            --drop-test.sql
            --example-ds.xml
        --resources-wildfly-remote
            --test-persistence.xml

Test and create script runs fine, but drop ain't happening, is anyone having the same issue?

EDIT (log added):

// log snippet discriminating that the drop command wasnt executed
2015-01-08 15:36:21,781 WARN  [org.hibernate.jpa.internal.schemagen.GenerationTargetToDatabase] (ServerService Thread Pool -- 65) Unable to execute JPA schema generation drop command [DROP TABLE table1]
2015-01-08 15:36:21,781 WARN  [org.hibernate.jpa.internal.schemagen.GenerationTargetToDatabase] (ServerService Thread Pool -- 65) Unable to execute JPA schema generation drop command [DROP TABLE table2]
2015-01-08 15:36:21,781 WARN  [org.hibernate.jpa.internal.schemagen.GenerationTargetToDatabase] (ServerService Thread Pool -- 65) Unable to execute JPA schema generation drop command [DROP TABLE table3]
2015-01-08 15:36:21,791 WARN  [org.hibernate.jpa.internal.schemagen.GenerationTargetToDatabase] (ServerService Thread Pool -- 65) Unable to execute JPA schema generation drop command [DROP TABLE table4]
2015-01-08 15:36:21,791 WARN  [org.hibernate.jpa.internal.schemagen.GenerationTargetToDatabase] (ServerService Thread Pool -- 65) Unable to execute JPA schema generation drop command [DROP TABLE table5]
2015-01-08 15:36:21,801 WARN  [org.hibernate.jpa.internal.schemagen.GenerationTargetToDatabase] (ServerService Thread Pool -- 65) Unable to execute JPA schema generation drop command [DROP TABLE table6]
2015-01-08 15:36:21,801 WARN  [org.hibernate.jpa.internal.schemagen.GenerationTargetToDatabase] (ServerService Thread Pool -- 65) Unable to execute JPA schema generation drop command [DROP TABLE table7]
2015-01-08 15:36:21,801 WARN  [org.hibernate.jpa.internal.schemagen.GenerationTargetToDatabase] (ServerService Thread Pool -- 65) Unable to execute JPA schema generation drop command [DROP TABLE table8]
2015-01-08 15:36:21,801 WARN  [org.hibernate.jpa.internal.schemagen.GenerationTargetToDatabase] (ServerService Thread Pool -- 65) Unable to execute JPA schema generation drop command [DROP TABLE table9]
2015-01-08 15:36:21,801 WARN  [org.hibernate.jpa.internal.schemagen.GenerationTargetToDatabase] (ServerService Thread Pool -- 65) Unable to execute JPA schema generation drop command [DROP TABLE table10]
2015-01-08 15:36:21,811 WARN  [org.hibernate.jpa.internal.schemagen.GenerationTargetToDatabase] (ServerService Thread Pool -- 65) Unable to execute JPA schema generation drop command [DROP TABLE table11]
2015-01-08 15:36:21,811 WARN  [org.hibernate.jpa.internal.schemagen.GenerationTargetToDatabase] (ServerService Thread Pool -- 65) Unable to execute JPA schema generation drop command [DROP TABLE table12]
2015-01-08 15:36:21,811 WARN  [org.hibernate.jpa.internal.schemagen.GenerationTargetToDatabase] (ServerService Thread Pool -- 65) Unable to execute JPA schema generation drop command [DROP TABLE table13]
2015-01-08 15:36:21,811 WARN  [org.hibernate.jpa.internal.schemagen.GenerationTargetToDatabase] (ServerService Thread Pool -- 65) Unable to execute JPA schema generation drop command [DROP TABLE table14]
2015-01-08 15:36:21,811 WARN  [org.hibernate.jpa.internal.schemagen.GenerationTargetToDatabase] (ServerService Thread Pool -- 65) Unable to execute JPA schema generation drop command [DROP TABLE table15]
2015-01-08 15:36:21,811 WARN  [org.hibernate.jpa.internal.schemagen.GenerationTargetToDatabase] (ServerService Thread Pool -- 65) Unable to execute JPA schema generation drop command [DROP TABLE table16]
2015-01-08 15:36:21,821 WARN  [org.hibernate.jpa.internal.schemagen.GenerationTargetToDatabase] (ServerService Thread Pool -- 65) Unable to execute JPA schema generation drop command [DROP TABLE table17]

I'll keep trying, any advance, will report here.

EDIT manual script exection at the @After test method:

protected static void dropAllTables(UserTransaction utx, EntityManager em)
        throws SystemException {
    // Temporary solution for table dropping after tests
    try {
        InputStream fis = Thread.currentThread().getContextClassLoader()
                .getResourceAsStream("META-INF/drop-teste.sql");

        StringBuilder builder = new StringBuilder();
        int ch;
        while ((ch = fis.read()) != -1) {
            builder.append((char) ch);
        }

        String[] dropCommands = builder.toString().split(";");

        utx.begin();
        for (String command : dropCommands) {
            em.createNativeQuery(command).executeUpdate();
        }
        utx.commit();
    } catch (Exception e) {
        utx.rollback();
    }
}
vcorrea
  • 101
  • 2
  • 8

1 Answers1

0

It looks like that Hibernate is trying to execute your drop commands, but is failing to do so.

It may happen due to constraints between tables. So please double check if order of deletion is good:

  • firstly: remove the table to of which there is no ForeignKey references.
  • secondly: remove next such table
  • and so on - until the most basic tables, which don't have any FKs inside.

Regars

G. Demecki
  • 10,145
  • 3
  • 58
  • 58
  • Hi Grzesiek, The odd thing, is that i'm using the exact same script, to delete the tables, i'm even executing the drop commands manually, reading the file, as a temporary solution, i'll edit the question with the snippet. – vcorrea Jan 09 '15 at 16:09
  • As for the constraint errors, already bumped into it, since i was executing it manually, and fixed. Anyway, thnx for the double checking : ) – vcorrea Jan 09 '15 at 16:17
  • Oh, and for the drop script, its written as simples as possible, like: `DROP TABLE table1; DROP TABLE table2; ...` – vcorrea Jan 09 '15 at 16:21