0

I'm testing (or trying to) my JPA/EJB classes against WildFly10 / H2 database using Arquillian. The primary key for an entity class is using GenerationType.TABLE. The persistence-unit in /META-INF/persistence.xml:

<persistence-unit name="test">
        <provider>org.hibernate.ejb.HibernatePersistence</provider>
        <jta-data-source>java:/H2DS</jta-data-source>
        <properties>
            <property name="hibernate.dialect" value="org.hibernate.dialect.HSQLDialect" />

            <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.create-script-source" value="META-INF/sql/create.sql" />
            <property name="javax.persistence.sql-load-script-source" value="META-INF/sql/load_script.sql" />

            <property name="javax.persistence.schema-generation.drop-source" value="script" />
            <property name="javax.persistence.schema-generation.drop-script-source" value="META-INF/sql/drop.sql" />
        </properties>

    </persistence-unit>

The problem is, the data that was supposed to be preloaded from load_script.sql is not committed by the time the test starts. I can see from logs the SQL statements to preload the test data, to update the "generator table", but I do not see any queries to the "generator table" to obtain a batch of primary keys. Then - no surprise - an attempt to add an entity ends up in constraint violation.

The question: how to make sure with Hibernate that the data preloaded from script is persisted before the tests take place?

Note 1: I had this issue with EclipseLink/Glassfish4, but that was remedied by both creating the schema and loading data from scripts (previously the schema was generated from entity classes).

Note 2: Without preloading any data the tests run fine both on Glassgish4 and WildFly10.

badbishop
  • 1,281
  • 2
  • 18
  • 38
  • Can you share with us your ShrinkWrap definition? Maybe you are not bundling the sql file. – lordofthejars Nov 07 '17 at 11:33
  • @lordofthejars: no, that is certainly not the case. I can see the inserts from the script in the logs. The problem is how to tell to JPA provider to: 1) flush data to generator table 2) then allocate a new bunch of IDs and update the table 3) then persist the entity from the test. At the moment, it tries to do all 3 steps inside the same transaction, but ignores the fact that some of pre-allocated IDs are already used for the entities from the script. – badbishop Nov 07 '17 at 12:08

1 Answers1

0

OK, a somewhat suboptimal solution is adding this implementation-specific option to persistence-unit configuration:

<property name="hibernate.id.new_generator_mappings" value="false"/>

I have posted another question that probably addresses the problem more precisely: JPA 2.1 testing: persisting the pre-loaded data

badbishop
  • 1,281
  • 2
  • 18
  • 38
  • See `javax.persistence.hibernate.hbm2ddl.import_files ` on http://docs.jboss.org/hibernate/orm/5.2/userguide/html_single/Hibernate_User_Guide.html#configurations-hbmddl. It's got a note "These statements are only executed if the schema is created, meaning that hibernate.hbm2ddl.auto is set to create, create-drop, or update." – James R. Perkins Nov 07 '17 at 23:09
  • @JamesR.Perkins: yes, thanks, I'm aware of that. Er... how is this relevant to the question? – badbishop Nov 08 '17 at 06:53
  • My apologies. I missed the `javax.persistence.schema-generation.database.action` property being set. – James R. Perkins Nov 08 '17 at 16:58