1
votes

I'm trying to create a project with glassfish JSF and JPA, but I'm having trouble with the persistence unit in the persistence.xml file.

The error is the following:

GlassFish 3.1 at localhost

2014-05-06T12:47:52.326-0300|INFO: Initializing Mojarra 2.1.6 (SNAPSHOT 20111206) for context '/K19-Integracao-JSF-JPA' 2014-05-06T12:47:52.649-0300|SEVERE: WebModule[/K19-Integracao-JSF-JPA]PWC1270: Exception starting filter br.com.k19.filters.JPAFilter java.lang.InstantiationException at org.apache.catalina.core.ApplicationFilterConfig.(ApplicationFilterConfig.java:124) at org.apache.catalina.core.StandardContext.filterStart(StandardContext.java:4685) at org.apache.catalina.core.StandardContext.start(StandardContext.java:5377) at com.sun.enterprise.web.WebModule.start(WebModule.java:498) at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:917) at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:901) at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:733) at com.sun.enterprise.web.WebContainer.loadWebModule(WebContainer.java:2018) at com.sun.enterprise.web.WebContainer.loadWebModule(WebContainer.java:1669) at com.sun.enterprise.web.WebApplication.start(WebApplication.java:109) at org.glassfish.internal.data.EngineRef.start(EngineRef.java:130) at org.glassfish.internal.data.ModuleInfo.start(ModuleInfo.java:269) at org.glassfish.internal.data.ApplicationInfo.start(ApplicationInfo.java:301) at com.sun.enterprise.v3.server.ApplicationLifecycle.deploy(ApplicationLifecycle.java:461) at com.sun.enterprise.v3.server.ApplicationLifecycle.deploy(ApplicationLifecycle.java:240) at org.glassfish.deployment.admin.DeployCommand.execute(DeployCommand.java:389) at com.sun.enterprise.v3.admin.CommandRunnerImpl$1.execute(CommandRunnerImpl.java:348) at com.sun.enterprise.v3.admin.CommandRunnerImpl.doCommand(CommandRunnerImpl.java:363) at com.sun.enterprise.v3.admin.CommandRunnerImpl.doCommand(CommandRunnerImpl.java:1085) at com.sun.enterprise.v3.admin.CommandRunnerImpl.access$1200(CommandRunnerImpl.java:95) at com.sun.enterprise.v3.admin.CommandRunnerImpl$ExecutionContext.execute(CommandRunnerImpl.java:1291) at com.sun.enterprise.v3.admin.CommandRunnerImpl$ExecutionContext.execute(CommandRunnerImpl.java:1259) at com.sun.enterprise.v3.admin.AdminAdapter.doCommand(AdminAdapter.java:461) at com.sun.enterprise.v3.admin.AdminAdapter.service(AdminAdapter.java:212) at com.sun.grizzly.tcp.http11.GrizzlyAdapter.service(GrizzlyAdapter.java:179) at com.sun.enterprise.v3.server.HK2Dispatcher.dispath(HK2Dispatcher.java:117) at com.sun.enterprise.v3.services.impl.ContainerMapper$Hk2DispatcherCallable.call(ContainerMapper.java:354) at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:195) at com.sun.grizzly.http.ProcessorTask.invokeAdapter(ProcessorTask.java:849) at com.sun.grizzly.http.ProcessorTask.doProcess(ProcessorTask.java:746) at com.sun.grizzly.http.ProcessorTask.process(ProcessorTask.java:1045) at com.sun.grizzly.http.DefaultProtocolFilter.execute(DefaultProtocolFilter.java:228) at com.sun.grizzly.DefaultProtocolChain.executeProtocolFilter(DefaultProtocolChain.java:137) at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:104) at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:90) at com.sun.grizzly.http.HttpProtocolChain.execute(HttpProtocolChain.java:79) at com.sun.grizzly.ProtocolChainContextTask.doCall(ProtocolChainContextTask.java:54) at com.sun.grizzly.SelectionKeyContextTask.call(SelectionKeyContextTask.java:59) at com.sun.grizzly.ContextTask.run(ContextTask.java:71) at com.sun.grizzly.util.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:532) at com.sun.grizzly.util.AbstractThreadPool$Worker.run(AbstractThreadPool.java:513) at java.lang.Thread.run(Thread.java:744) Caused by: javax.persistence.PersistenceException: No Persistence provider for EntityManager named k19pu at javax.persistence.Persistence.createEntityManagerFactory(Unknown Source) at javax.persistence.Persistence.createEntityManagerFactory(Unknown Source) at br.com.k19.filters.JPAFilter.init(JPAFilter.java:57) at org.apache.catalina.core.ApplicationFilterConfig.getFilter(ApplicationFilterConfig.java:264) at org.apache.catalina.core.ApplicationFilterConfig.(ApplicationFilterConfig.java:120) ... 41 more

2014-05-06T12:47:52.662-0300|INFO: WEB0671: Loading application [K19-Integracao-JSF-JPA] at [/K19-Integracao-JSF-JPA] 2014-05-06T12:47:52.693-0300|INFO: K19-Integracao-JSF-JPA was successfully deployed in 774 milliseconds. 2014-05-06T12:47:53.049-0300|SEVERE: Error Rendering View[/carros.xhtml] javax.el.ELException: /carros.xhtml @24,58 rendered="#{not empty carroBean.carros}": java.lang.NullPointerException at com.sun.faces.facelets.el.TagValueExpression.getValue(TagValueExpression.java:114) at javax.faces.component.ComponentStateHelper.eval(ComponentStateHelper.java:194) at javax.faces.component.UIComponentBase.isRendered(UIComponentBase.java:413) at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1751) at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1760) at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1760) at com.sun.faces.application.view.FaceletViewHandlingStrategy.renderView(FaceletViewHandlingStrategy.java:402) at com.sun.faces.application.view.MultiViewHandler.renderView(MultiViewHandler.java:131) at com.sun.faces.lifecycle.RenderResponsePhase.execute(RenderResponsePhase.java:121) at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101) at com.sun.faces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:139) at javax.faces.webapp.FacesServlet.service(FacesServlet.java:594) at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1542) at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:281) at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175) at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:655) at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:595) at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:161) at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:331) at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:231) at com.sun.enterprise.v3.services.impl.ContainerMapper$AdapterCallable.call(ContainerMapper.java:317) at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:195) at com.sun.grizzly.http.ProcessorTask.invokeAdapter(ProcessorTask.java:849) at com.sun.grizzly.http.ProcessorTask.doProcess(ProcessorTask.java:746) at com.sun.grizzly.http.ProcessorTask.process(ProcessorTask.java:1045) at com.sun.grizzly.http.DefaultProtocolFilter.execute(DefaultProtocolFilter.java:228) at com.sun.grizzly.DefaultProtocolChain.executeProtocolFilter(DefaultProtocolChain.java:137) at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:104) at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:90) at com.sun.grizzly.http.HttpProtocolChain.execute(HttpProtocolChain.java:79) at com.sun.grizzly.ProtocolChainContextTask.doCall(ProtocolChainContextTask.java:54) at com.sun.grizzly.SelectionKeyContextTask.call(SelectionKeyContextTask.java:59) at com.sun.grizzly.ContextTask.run(ContextTask.java:71) at com.sun.grizzly.util.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:532) at com.sun.grizzly.util.AbstractThreadPool$Worker.run(AbstractThreadPool.java:513) at java.lang.Thread.run(Thread.java:744) Caused by: java.lang.NullPointerException at br.com.k19.modelo.CarroRepository.buscaTodos(CarroRepository.java:21) at br.com.k19.controle.CarroBean.getCarros(CarroBean.java:30) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:606) at javax.el.BeanELResolver.getValue(BeanELResolver.java:363) at com.sun.faces.el.DemuxCompositeELResolver._getValue(DemuxCompositeELResolver.java:176) at com.sun.faces.el.DemuxCompositeELResolver.getValue(DemuxCompositeELResolver.java:203) at com.sun.el.parser.AstValue.getValue(AstValue.java:138) at com.sun.el.parser.AstValue.getValue(AstValue.java:183) at com.sun.el.parser.AstEmpty.getValue(AstEmpty.java:66) at com.sun.el.parser.AstNot.getValue(AstNot.java:63) at com.sun.el.ValueExpressionImpl.getValue(ValueExpressionImpl.java:224) at com.sun.faces.facelets.el.TagValueExpression.getValue(TagValueExpression.java:109) ... 35 more

2014-05-06T12:47:53.053-0300|WARNING: StandardWrapperValve[Faces Servlet]: PWC1406: Servlet.service() for servlet Faces Servlet threw exception java.lang.NullPointerException at br.com.k19.modelo.CarroRepository.buscaTodos(CarroRepository.java:21) at br.com.k19.controle.CarroBean.getCarros(CarroBean.java:30) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:606) at javax.el.BeanELResolver.getValue(BeanELResolver.java:363) at com.sun.faces.el.DemuxCompositeELResolver._getValue(DemuxCompositeELResolver.java:176) at com.sun.faces.el.DemuxCompositeELResolver.getValue(DemuxCompositeELResolver.java:203) at com.sun.el.parser.AstValue.getValue(AstValue.java:138) at com.sun.el.parser.AstValue.getValue(AstValue.java:183) at com.sun.el.parser.AstEmpty.getValue(AstEmpty.java:66) at com.sun.el.parser.AstNot.getValue(AstNot.java:63) at com.sun.el.ValueExpressionImpl.getValue(ValueExpressionImpl.java:224) at com.sun.faces.facelets.el.TagValueExpression.getValue(TagValueExpression.java:109) at javax.faces.component.ComponentStateHelper.eval(ComponentStateHelper.java:194) at javax.faces.component.UIComponentBase.isRendered(UIComponentBase.java:413) at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1751) at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1760) at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1760) at com.sun.faces.application.view.FaceletViewHandlingStrategy.renderView(FaceletViewHandlingStrategy.java:402) at com.sun.faces.application.view.MultiViewHandler.renderView(MultiViewHandler.java:131) at com.sun.faces.lifecycle.RenderResponsePhase.execute(RenderResponsePhase.java:121) at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101) at com.sun.faces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:139) at javax.faces.webapp.FacesServlet.service(FacesServlet.java:594) at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1542) at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:281) at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175) at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:655) at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:595) at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:161) at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:331) at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:231) at com.sun.enterprise.v3.services.impl.ContainerMapper$AdapterCallable.call(ContainerMapper.java:317) at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:195) at com.sun.grizzly.http.ProcessorTask.invokeAdapter(ProcessorTask.java:849) at com.sun.grizzly.http.ProcessorTask.doProcess(ProcessorTask.java:746) at com.sun.grizzly.http.ProcessorTask.process(ProcessorTask.java:1045) at com.sun.grizzly.http.DefaultProtocolFilter.execute(DefaultProtocolFilter.java:228) at com.sun.grizzly.DefaultProtocolChain.executeProtocolFilter(DefaultProtocolChain.java:137) at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:104) at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:90) at com.sun.grizzly.http.HttpProtocolChain.execute(HttpProtocolChain.java:79) at com.sun.grizzly.ProtocolChainContextTask.doCall(ProtocolChainContextTask.java:54) at com.sun.grizzly.SelectionKeyContextTask.call(SelectionKeyContextTask.java:59) at com.sun.grizzly.ContextTask.run(ContextTask.java:71) at com.sun.grizzly.util.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:532) at com.sun.grizzly.util.AbstractThreadPool$Worker.run(AbstractThreadPool.java:513) at java.lang.Thread.run(Thread.java:744)


And the classes I am using are as follows:

package br.com.k19.modelo;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.Query;

public class CarroRepository {

    private EntityManager manager;

    public CarroRepository(EntityManager manager) {
        this.manager = manager;
    }

    public void adiciona(Carro carro) {
        this.manager.persist(carro);
    }

    public  List<Carro> buscaTodos() {
        Query query = this.manager.createQuery("select x from Carro x");
        return query.getResultList();
    }
}

package br.com.k19.modelo;

import java.io.Serializable;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;

@Entity
public class Carro implements Serializable {

    /**
     * 
     */
    private static final long serialVersionUID = 4209908627808946183L;

    @Id @GeneratedValue
    private Long id;
    private String marca;
    private String modelo;

    public Long getId() {
        return id;
    }
    public void setId(Long id) {
        this.id = id;
    }
    public String getMarca() {
        return marca;
    }
    public void setMarca(String marca) {
        this.marca = marca;
    }
    public String getModelo() {
        return modelo;
    }
    public void setModelo(String modelo) {
        this.modelo = modelo;
    }
    public static long getSerialversionuid() {
        return serialVersionUID;
    }   
}

package br.com.k19.filters;

import java.io.IOException;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;


@WebFilter(servletNames={"Faces Servlet"})
public class JPAFilter implements Filter {

    private EntityManagerFactory factory;

    @Override
    public void destroy() {

        this.factory.close();
    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response,
            FilterChain chain) throws IOException, ServletException {

        // Chegada
        EntityManager manager = factory.createEntityManager();
        request.setAttribute("EntityManager", manager);
        manager.getTransaction().begin();
        // Chegada

        // Faces Servelet
        chain.doFilter(request, response);
        // Faces Servelet

        // Saída
        try {
            manager.getTransaction().commit();
        } catch(Exception e) {
            manager.getTransaction().rollback();
        } finally {
            manager.close();
        }
        // Saída

    }

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

        this.factory = Persistence.createEntityManagerFactory("K19-PU");
    }
}

package br.com.k19.controle;

import java.util.List;

import javax.faces.bean.ManagedBean;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.persistence.EntityManager;
import javax.servlet.http.HttpServletRequest;

import br.com.k19.modelo.Carro;
import br.com.k19.modelo.CarroRepository;

@ManagedBean
public class CarroBean {

    private Carro carro = new Carro();

    public void adicionaCarro() {
        EntityManager manager = this.getEntityManager();
        CarroRepository repository = new CarroRepository(manager);

        repository.adiciona(carro);
        carro = new Carro();
    }

    public List<Carro> getCarros() {
        EntityManager manager = this.getEntityManager();
        CarroRepository repository = new CarroRepository(manager);
        return repository.buscaTodos();
    }

    private EntityManager getEntityManager() {

        FacesContext fc = FacesContext.getCurrentInstance();
        ExternalContext ec = fc.getExternalContext();
        HttpServletRequest request = (HttpServletRequest) ec.getRequest();
        EntityManager manager = (EntityManager) request.getAttribute("EntityManager");

        return manager;
    }

    public Carro getCarro() {
        return carro;
    }

    public void setCarro(Carro carro) {
        this.carro = carro;
    }
}

And the file carro.xhtml

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:f="http://java.sun.com/jsf/core"
      xmlns:ui="http://java.sun.com/jsf/facelets"> 

<h:head>
    <title>K19 treinamentos</title>
</h:head> 
<h:body> 
    <h:form>
        <h:panelGrid columns="2">

            <h:outputLabel value="Marca: " for="campo-marca" />
            <h:inputText value="#{carroBean.carro.marca}" id="campo-marca" />

            <h:outputLabel value="Modelo: " for="campo-modelo" />
            <h:inputText value="#{carroBean.carro.modelo}" id="campo-modelo" />

            <h:commandButton action="#{carroBean.adicionaCarro}" value="Adicionar" />
        </h:panelGrid>
    </h:form>

    <h:panelGroup rendered="#{not empty carroBean.carros}" >
        <h1>Lista de Carros</h1>

        <ul>
            <ui:repeat value="#{carroBean.carros}" var="carro">
                <li> <h:outputText value="#{carro.marca} #{carro.modelo}" /> </li>
            </ui:repeat>
        </ul>
    </h:panelGroup>
</h:body> 
</html>

My persistence.xml looks like this:

<?xml version="1.0" encoding="UTF-8"?>

<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">

    <persistence-unit name="K19-PU" transaction-type="RESOURCE_LOCAL">

        <provider>org.hibernate.ejb.HibernatePersistence</provider>
        <properties>
            <property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5InnoDBDialect" />
            <property name="hibernate.hbm2ddl.auto" value="create" />
            <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver" />
            <property name="javax.persistence.jdbc.user" value="root" />
            <property name="javax.persistence.jdbc.password" value="admin" />
            <property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/K19DB"/>
        </properties>
    </persistence-unit>
</persistence>

My persistence.xml in this folder: ~/workspace/K19-Integracao-JSF-JPA/src/META-INF/persistence.xml

I put the mysql connector jar in the glassfish directory: /opt/glassfish3/glassfish/lib/mysql-connector-java-5.1.30-bin.jar

I'm sorry for posting so much, is that I'm already crazy with this error.

I already searched a lot on the internet and can not resolve the problem, please can someone help me?

2
Some parts of the code has words in Portuguese but I think this is irrelevant to you.Patterson

2 Answers

1
votes

I think should always look at the root cause of exception. In your case

Caused by: javax.persistence.PersistenceException: 
No Persistence provider for EntityManager named k19pu at javax.persistence.Persistence.createEntityManagerFactory 

Your persistence.xml is not valid that's why EntityManagerFactory can't be get created.

due this you are also getting NLP at CarroRepository#buscaTodos method

Caused by: java.lang.NullPointerException at br.com.k19.modelo.CarroRepository.buscaTodos

So Please correct your Persistence.xml and make sure same PU. hope this will solve your problem

0
votes

Here's the problem:

2014-05-06T12:47:53.049-0300|SEVERE: Error Rendering View[/carros.xhtml] javax.el.ELException: /carros.xhtml @24,58 rendered="#{not empty carroBean.carros}": java.lang.NullPointerException

...

Caused by: java.lang.NullPointerException at br.com.k19.modelo.CarroRepository.buscaTodos(CarroRepository.java:21) at br.com.k19.controle.CarroBean.getCarros(CarroBean.java:30)

Looking until CarroRepository#buscaTodos, line 21 of CarroRepository.java: file, we get this line:

Query query = this.manager.createQuery("select x from Carro x");

The NullPointerException means that EntityManager manager is null.

You have to check why the EntityManagerFactory cannot create a new instance of EntityManager in your JPAFilter servlet filter.