Gerardo Tarragona Gerardo Tarragona - 1 year ago 127
Java Question

Can't use EJB from Web Application

I have the next problem while trying to develope a REST app using JPA and EJB's.

I have 3 projects in NetBeans (same workspace, just projects, not EAR or that weird stuff):

  • One of them is a Java Application in which I created some entity classes from a database. Inside of the same project, I created other package containing the controller classes from the entities. I tested the project creating a java main class and using the controllers.

  • The other project is a EJB module, it as simple as a package and the next class inside of it:

    package com.studentejb;
    import java.util.List;
    import javax.ejb.LocalBean;
    import javax.ejb.Stateless;
    import studentdao.StudentTO;
    import studentdao.StudentJpaController;

    public class StudentEJB {

    private StudentJpaController studentDao = new StudentJpaController();

    public List<StudentTO> findStudents() {
    return studentDao.findStudentTO();

As you can see, I imported the previous JAR project (the one with the entity's controllers). I also tested this EJB project with a java main class and it worked.

  • The last project is a web application, as you can imagine I want to use the EJB modules of the previous project but I get the following errors while trying to enter to the resource

    12:00:12,384 ERROR [io.undertow.request] (default task-2) UT005023: Exception handling request to /studentWeb/student/hola: java.lang.ExceptionInInitializerError
    at org.jboss.weld.util.cache.LoadingCacheUtils.getCacheValue(

    Caused by: java.lang.ExceptionInInitializerError
    at studentdao.StudentJpaController.<init>(
    at com.studentejb.StudentEJB.<init>(
    at com.studentweb.Resources.<init>(
    at com.studentweb.Resources$Proxy$_$$_WeldClientProxy.<init>(Unknown Source)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)

    ... 58 more
    Caused by: javax.persistence.PersistenceException: Unable to locate persistence units
    at org.hibernate.jpa.HibernatePersistenceProvider.getEntityManagerFactoryBuilderOrNull(
    at org.hibernate.jpa.HibernatePersistenceProvider.getEntityManagerFactoryBuilderOrNull(
    at org.hibernate.jpa.HibernatePersistenceProvider.createEntityManagerFactory(
    at javax.persistence.Persistence.createEntityManagerFactory(
    at javax.persistence.Persistence.createEntityManagerFactory(
    at studentdao.EntityProvider.<clinit>(
    ... 80 more

    Caused by: javax.persistence.PersistenceException: Invalid persistence.xml.
    Error parsing XML [line : -1, column : -1] : cvc-elt.1.a: Cannot find the declaration of element 'persistence'.

    at org.hibernate.jpa.boot.internal.PersistenceXmlParser.validate(
    at org.hibernate.jpa.boot.internal.PersistenceXmlParser.loadUrl(
    at org.hibernate.jpa.boot.internal.PersistenceXmlParser.parsePersistenceXml(
    at org.hibernate.jpa.boot.internal.PersistenceXmlParser.doResolve(
    at org.hibernate.jpa.boot.internal.PersistenceXmlParser.locatePersistenceUnits(
    at org.hibernate.jpa.HibernatePersistenceProvider.getEntityManagerFactoryBuilderOrNull(
    ... 85 more

Here is the code of the web app (

public class Resources {
private StudentEJB studentEjb;

private UriInfo context;

public Resources() throws FileNotFoundException, IOException, NamingException {
studentEjb = new StudentEJB();

public Response getJson() {
return Response.ok(studentEjb.findStudents()).build();

So I am using 2 JARs for the web project, the one with the EJB modules and the other with the entities and controllers..

Here is the persistence.xml generated by the JPA project:

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0" xmlns="" xmlns:xsi="" xsi:schemaLocation="">
<persistence-unit name="studentJPAPU" transaction-type="RESOURCE_LOCAL">
<!-- <provider>org.hibernate.ejb.HibernatePersistence</provider> -->
<property name="javax.persistence.jdbc.url" value="jdbc:sqlserver://localhost:1433;databaseName=tca_student_db;integratedSecurity=true"/>
<property name="javax.persistence.jdbc.user" value="xxxx"/>
<property name="javax.persistence.jdbc.driver" value=""/>
<property name="javax.persistence.jdbc.password" value="xxxx"/>

Thank you!

Answer Source

Caused by: javax.persistence.PersistenceException: Invalid persistence.xml. Error parsing XML [line : -1, column : -1] : cvc-elt.1.a: Cannot find the declaration of element 'persistence'.

Here :

<persistence version="2.0" xmlns=""  

xmlns : doesn't match with the namespace in xsischemaLocation which is

Try to use the same in both :

<persistence version="2.0" xmlns=""   
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download