JPA 2.1 Удаление выполнения сценария из файла persistence.xml с помощью Arquillian и Shrinkwrap

Я пишу несколько тестов 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
// 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");
}
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

Сценарий тестирования и создания работает нормально, но сброса не происходит, у кого-нибудь есть такая же проблема?

EDIT (добавлен журнал):

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

person vcorrea    schedule 08.01.2015    source источник


Ответы (1)


Похоже, что Hibernate пытается выполнить ваши команды перетаскивания, но не может этого сделать.

Это может произойти из-за ограничений между таблицами. Поэтому, пожалуйста, дважды проверьте, правильный ли порядок удаления:

  • во-первых: удалите таблицу, на которую нет ссылок ForeignKey.
  • во-вторых: удалить следующую такую ​​таблицу
  • и так далее - до самых простых таблиц, внутри которых нет FK.

С уважением

person G. Demecki    schedule 09.01.2015
comment
Привет, Гжесик. Странно то, что я использую тот же самый скрипт, чтобы удалить таблицы, я даже выполняю команды удаления вручную, читая файл, в качестве временного решения я отредактирую вопрос с помощью фрагмент. - person vcorrea; 09.01.2015
comment
Что касается ошибок ограничения, я уже столкнулся с этим, так как я выполнял его вручную и исправил. В любом случае, спасибо за двойную проверку :) - person vcorrea; 09.01.2015
comment
О, и сценарий дропа написан настолько просто, насколько это возможно, например: DROP TABLE table1; DROP TABLE table2; ... - person vcorrea; 09.01.2015