Deploying Hibernate app to Wildfly 10 - org.dom4j.DocumentFactory ClassCastException
<p><strong>UPDATE:</strong> After destroying the WildFly instance and reconfiguring from scratch, the error has mysteriously resolved itself into a <code>org.hibernate.MappingException</code> for repeated columns.  Now that I actually know what the problem is, I can get to work fixing it.</p>

<hr>

<p>I'm currently evaluating JPA backed by Hibernate in WildFly 10.  However, on attempting to deploy my test war (built by gradle) a seemingly irrelevant error is produced.  Here are the lines from the console log:</p>

<blockquote>
  <p>11:11:06,200 ERROR [org.jboss.as.controller.management-operation] (management task-8) WFLYCTL0013: Operation ("add") failed - address: ([("deployment" => "TestWar.war")]) - failure description: {"WFLYCTL0080: Failed services" => {"jboss.persistenceunit.\"TestWar.war#com.myapp.testwar.jpa\"" => "org.jboss.msc.service.StartException in service jboss.persistenceunit.\"TestWar.war#com.myapp.testwar.jpa\": java.lang.ClassCastException: org.dom4j.DocumentFactory cannot be cast to org.dom4j.DocumentFactory</p>
  
  <p>Caused by: java.lang.ClassCastException: org.dom4j.DocumentFactory cannot be cast to org.dom4j.DocumentFactory"}}</p>
</blockquote>

<p>From my <code>build.gradle</code>:</p>

<pre><code>group 'com.myapp'
name 'TestWar'
version '0.0.0-dev'

apply plugin: 'java'
apply plugin: 'war'

sourceCompatibility = 1.8

repositories {
    mavenCentral()
}

configurations {
    provided
}

sourceSets {
    main {
        compileClasspath += configurations.provided
    }
}

dependencies {
    provided group: 'dom4j', name: 'dom4j', version: '1.6.1'
    provided group: 'javax.enterprise', name: 'cdi-api', version: '1.2'
    provided group: 'javax.inject', name: 'javax.inject', version: '1'
    provided group: 'org.hibernate', name: 'hibernate-core', version: '5.0.7.Final'
    provided group: 'org.hibernate', name: 'hibernate-entitymanager', version: '5.0.7.Final'
    provided group: 'org.hibernate', name: 'hibernate-envers', version: '5.0.7.Final'
    provided group: 'org.hibernate', name: 'hibernate-java8', version: '5.0.7.Final'
    provided group: 'org.hibernate.javax.persistence', name: 'hibernate-jpa-2.1-api', version: '1.0.0.Final'
    provided group: 'org.jboss.spec.javax.annotation', name: 'jboss-annotations-api_1.2_spec', version: '1.0.0.Final'
    provided group: 'org.jboss.spec.javax.ejb', name: 'jboss-ejb-api_3.2_spec', version: '1.0.0.Final'
    provided group: 'org.jboss.spec.javax.sql', name: 'jboss-javax-sql-api_7.0_spec', version: '2.0.0.Final'
    provided group: 'org.jboss.spec.javax.ws.rs', name: 'jboss-jaxrs-api_2.0_spec', version: '1.0.0.Final'
    provided group: 'org.postgresql', name: 'postgresql', version: '9.4.1209'
}
</code></pre>

<p>I have tried the following solutions from similar posts on SO:</p>

<ul>
<li><a href="https://stackoverflow.com/a/35692287/1813169">Making all dependencies provided</a></li>
<li><a href="https://stackoverflow.com/a/25877129/1813169">Adding <code>dom4j</code> to my dependencies</a> (seems unnecessary since <code>gradle dependencies</code> shows it as a child of multiple hibernate libraries)</li>
<li><a href="https://stackoverflow.com/a/1804334/1813169">Ensure there is only one <code>dom4j</code> jar in the classpath</a> (there are no libraries in my war at all)</li>
</ul>

<p>As well as these from other sources:</p>

<ul>
<li><a href="https://developer.jboss.org/message/630478#630478" rel="nofollow noreferrer">Adding <code>Dependencies: org.dom4j</code> to MANIFEST.MF</a></li>
<li><a href="https://issues.jboss.org/browse/WFLY-5549" rel="nofollow noreferrer">Adding <code>jboss-deployment-structure.xml</code> with the following content:</a></li>
</ul>

<p>&nbsp;</p>

<pre><code>&lt;jboss-deployment-structure xmlns="urn:jboss:deployment-structure:1.2"&gt;
  &lt;deployment&gt;
    &lt;dependencies&gt;
      &lt;module name="org.dom4j"/&gt;
    &lt;/dependencies&gt;
    &lt;local-last value="true"/&gt;
  &lt;/deployment&gt;
&lt;/jboss-deployment-structure&gt;
</code></pre>

<p>Does anyone have any further suggestions, or have I done something monumentally stupid?</p>

<hr>

<p><strong>Edit:</strong> Apparently this error can be caused by any datasource configuration issues (see <a href="https://developer.jboss.org/message/756300#756300" rel="nofollow noreferrer">here</a>).  For reference, the connection tests for my datasource via the admin console are successful, and the connection details are all correct as far as I'm aware.</p>