How to Solve the Error Message: Unexpected element ‘{http://java.sun.com/xml/ns/persistence}jta-data-source’ encountered

Posted on

This article specifically written in order to try to give an answer and further explain on how to solve the error specified in the title of this article itself. The actual error is happened and it has been simulated to be solved. The error itself is triggered upon creating a Java-based application in an IDE called Netbeans. The actual problem lies on the persistence.xml file which is used as a definition for a JPA Persistence Unit. The file itself which is an XML file, it has a certain structure which must be well-defined. Below is the main problem which is suspected to be the culprit of the error triggered :

<jta-data-source>java:/myDS</jta-data-source>

The actual position of the above line in the persistence.xml which cause the error for being triggered is shown below :

<properties>
<!-- custom scanner test -->
<jta-data-source>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/>
<property name="hibernate.hbm2ddl.auto" value="create-drop"/>
<property name="hibernate.show_sql" value="false" />
</jta-data-source>
</properties>

It is defined in an improper sequence of the structure of the XML file. The location of the tag XML above is specified or it has been defined in the persistence file named persistence.xml. To be more precise, below is the actual location of the persistence.xml file :

user@hostname:~/NetBeansProjects/webapp$ tree -L 5
.
├── nb-configuration.xml
├── pom.xml
├── src
│   ├── main
│   │   ├── java
│   │   │   ├── app
│   │   │   │   └── webapp
│   │   │   └── MainDirector.java
│   │   ├── resources
│   │   │   └── META-INF
│   │   │       └── persistence.xml
│   │   └── webapp
│   │       ├── index.html
│   │       ├── index.jsp
│   │       ├── server-add.jsp
│   │       ├── server-detail.jsp
│   │       └── WEB-INF
│   │           ├── jboss-deployment-structure.xml
│   │           └── web.xml
│   └── test
│       └── java
└── target
    ├── classes
    │   ├── app
    │   │   └── webapp
    │   │       ├── bean
    │   │       ├── client
    │   │       ├── controller
    │   │       └── entity
    │   ├── MainDirector.class
    │   └── META-INF
    │       └── persistence.xml
    ... 
    └── webapp-1.0-SNAPSHOT.war

39 directories, 95 files
user@hostname:~/NetBeansProjects/webapp$ 

So, in order to solve the above problem, figuring out the error message given which is the ‘Unexpected element’ part of  the tag element ‘jta-data-source’ encountered’ which is assumed it is not correct, so it is most likely that  the structure for the placement of the tag element ‘jta-data-source’ is not accordance with the structure specified in the ‘http://java.sun.com/xml/ns/persistence’ , the only way to fix it is done by removing the tag element or placing it in the other part of the content.

Below it is the actual persistence.xml content which is worked :

<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
<persistence-unit name="primary" transaction-type="JTA">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<jta-data-source>java:/jdbc/testing-database</jta-data-source>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5InnoDBDialect"/>
<property name="hibernate.hbm2ddl.auto" value="create"/>
<property name="hibernate.show_sql" value="true" />
<property name="hibernate.current_session_context_class">thread</property>
</properties>
</persistence-unit>
</persistence>

As shown in the above script, the tag ‘<jta-data-source>’ is being located into the outside of the ‘<properties’> tag. The error is solved by only editing the persistence.xml into the above structure.

Leave a Reply