Gerardo Tarragona Gerardo Tarragona - 1 month ago 9
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;

    @Stateless
    @LocalBean
    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
    http://localhost:8080/studentWeb/student/
    :

    12:00:12,384 ERROR [io.undertow.request] (default task-2) UT005023: Exception handling request to /studentWeb/student/hola: com.google.common.util.concurrent.ExecutionError: java.lang.ExceptionInInitializerError
    at com.google.common.cache.LocalCache$Segment.get(LocalCache.java:2201)
    at com.google.common.cache.LocalCache.get(LocalCache.java:3937)
    at com.google.common.cache.LocalCache.getOrLoad(LocalCache.java:3941)
    at com.google.common.cache.LocalCache$LocalLoadingCache.get(LocalCache.java:4824)
    at org.jboss.weld.util.cache.LoadingCacheUtils.getCacheValue(LoadingCacheUtils.java:49)
    .
    .
    .

    at java.lang.Thread.run(Thread.java:745)
    Caused by: java.lang.ExceptionInInitializerError
    at studentdao.StudentJpaController.<init>(StudentJpaController.java:40)
    at com.studentejb.StudentEJB.<init>(StudentEJB.java:23)
    at com.studentweb.Resources.<init>(Resources.java:52)
    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(HibernatePersistenceProvider.java:99)
    at org.hibernate.jpa.HibernatePersistenceProvider.getEntityManagerFactoryBuilderOrNull(HibernatePersistenceProvider.java:86)
    at org.hibernate.jpa.HibernatePersistenceProvider.createEntityManagerFactory(HibernatePersistenceProvider.java:67)
    at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:55)
    at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:39)
    at studentdao.EntityProvider.<clinit>(EntityProvider.java:20)
    ... 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(PersistenceXmlParser.java:377)
    at org.hibernate.jpa.boot.internal.PersistenceXmlParser.loadUrl(PersistenceXmlParser.java:310)
    at org.hibernate.jpa.boot.internal.PersistenceXmlParser.parsePersistenceXml(PersistenceXmlParser.java:114)
    at org.hibernate.jpa.boot.internal.PersistenceXmlParser.doResolve(PersistenceXmlParser.java:104)
    at org.hibernate.jpa.boot.internal.PersistenceXmlParser.locatePersistenceUnits(PersistenceXmlParser.java:86)
    at org.hibernate.jpa.HibernatePersistenceProvider.getEntityManagerFactoryBuilderOrNull(HibernatePersistenceProvider.java:95)
    ... 85 more



Here is the code of the web app (Resources.java):

@Path("/student")
public class Resources {
@EJB
private StudentEJB studentEjb;

@Context
private UriInfo context;

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

@GET
@Path("/")
@Produces(MediaType.APPLICATION_JSON)
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="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
<persistence-unit name="studentJPAPU" transaction-type="RESOURCE_LOCAL">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<!-- <provider>org.hibernate.ejb.HibernatePersistence</provider> -->
<class>studentjpa.Address</class>
<class>studentjpa.Phone</class>
<class>studentjpa.Student</class>
<class>studentjpa.Email</class>
<properties>
<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="com.microsoft.sqlserver.jdbc.SQLServerDriver"/>
<property name="javax.persistence.jdbc.password" value="xxxx"/>
</properties>
</persistence-unit>
</persistence>


Thank you!

Answer

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="http://xmlns.jcp.org/xml/ns/persistence"  
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">

xmlns :http://xmlns.jcp.org/xml/ns/persistence doesn't match with the namespace in xsischemaLocation which is http://java.sun.com/xml/ns/persistence

Try to use the same in both :

<persistence version="2.0" 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 
  http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
Comments