0

我正在编写一些 Arquillian(使用 Shrinkwrap)测试,我目前正在使用 JPA 2.1 通过 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 片段:

// 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
    --src/test
        --java
            --com/example/MyTest.java
        --resources
            --create-test.sql
            --drop-test.sql
            --example-ds.xml
        --resources-wildfly-remote
            --test-persistence.xml

测试和创建脚本运行良好,但没有发生下降,有人有同样的问题吗?

编辑(添加日志):

// 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]

我会继续努力,任何进展都会在这里报告。

在@After 测试方法中编辑手动脚本执行:

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

1 回答 1

0

看起来 Hibernate 正在尝试执行您的 drop 命令,但没有这样做。

这可能是由于表之间的约束而发生的。所以请仔细检查删除顺序是否正确:

  • 首先:删除没有 ForeignKey 引用的表。
  • 其次:删除下一个这样的表
  • 依此类推——直到最基本的表格,里面没有任何 FK。

问候

于 2015-01-09T07:52:16.797 回答