javax.servlet.Servletexception: java.lang.Exceptionininitializererro

Asked

Viewed 187 times

1

I’m trying to create my first project using JPA, using Jboss 5, Oracle, Hibernate and primefaces. After deploying the project I try to create and insert a new client into the bank through the web interface, but am getting the following error:

   exception

javax.servlet.ServletException: java.lang.ExceptionInInitializerError
    javax.faces.webapp.FacesServlet.service(FacesServlet.java:606)
    org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:96)

root cause

javax.faces.el.EvaluationException: java.lang.ExceptionInInitializerError
    javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:102)
    com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:102)
    javax.faces.component.UICommand.broadcast(UICommand.java:315)
    javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:794)
    javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1259)
    com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:81)
    com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
    com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:118)
    javax.faces.webapp.FacesServlet.service(FacesServlet.java:593)
    org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:96)

root cause

java.lang.ExceptionInInitializerError
    Beans.ClienteBean.insertPedido(ClienteBean.java:27)
    sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    java.lang.reflect.Method.invoke(Unknown Source)
    org.apache.el.parser.AstValue.invoke(AstValue.java:170)
    org.apache.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:276)
    com.sun.faces.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:105)
    javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:88)
    com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:102)
    javax.faces.component.UICommand.broadcast(UICommand.java:315)
    javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:794)
    javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1259)
    com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:81)
    com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
    com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:118)
    javax.faces.webapp.FacesServlet.service(FacesServlet.java:593)
    org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:96)

root cause

javax.persistence.PersistenceException: [PersistenceUnit: MC] Unable to build EntityManagerFactory
    org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:677)
    org.hibernate.ejb.HibernatePersistence.createEntityManagerFactory(HibernatePersistence.java:126)
    javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:52)
    javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:34)
    com.bd.JpaUtil.<clinit>(JpaUtil.java:10)
    Beans.ClienteBean.insertPedido(ClienteBean.java:27)
    sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    java.lang.reflect.Method.invoke(Unknown Source)
    org.apache.el.parser.AstValue.invoke(AstValue.java:170)
    org.apache.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:276)
    com.sun.faces.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:105)
    javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:88)
    com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:102)
    javax.faces.component.UICommand.broadcast(UICommand.java:315)
    javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:794)
    javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1259)
    com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:81)
    com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
    com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:118)
    javax.faces.webapp.FacesServlet.service(FacesServlet.java:593)
    org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:96)

root cause

org.hibernate.cache.NoCachingEnabledException: Second-level cache is not enabled for usage [hibernate.cache.use_second_level_cache | hibernate.cache.use_query_cache]
    org.hibernate.cache.impl.NoCachingRegionFactory.buildTimestampsRegion(NoCachingRegionFactory.java:79)
    org.hibernate.cache.UpdateTimestampsCache.<init>(UpdateTimestampsCache.java:58)
    org.hibernate.impl.SessionFactoryImpl.<init>(SessionFactoryImpl.java:369)
    org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1327)
    org.hibernate.cfg.AnnotationConfiguration.buildSessionFactory(AnnotationConfiguration.java:867)
    org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:669)
    org.hibernate.ejb.HibernatePersistence.createEntityManagerFactory(HibernatePersistence.java:126)
    javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:52)
    javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:34)
    com.bd.JpaUtil.<clinit>(JpaUtil.java:10)
    Beans.ClienteBean.insertPedido(ClienteBean.java:27)
    sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    java.lang.reflect.Method.invoke(Unknown Source)
    org.apache.el.parser.AstValue.invoke(AstValue.java:170)
    org.apache.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:276)
    com.sun.faces.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:105)
    javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:88)
    com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:102)
    javax.faces.component.UICommand.broadcast(UICommand.java:315)
    javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:794)
    javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1259)
    com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:81)
    com.sun.faces.lif`enter code here`ecycle.Phase.doPhase(Phase.java:101)
    com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:118)
    javax.faces.webapp.FacesServlet.service(FacesServlet.java:593)
    org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:96

My persistence.xml:

<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="MC" transaction-type="JTA">
    <provider>org.hibernate.ejb.HibernatePersistence</provider>
    <jta-data-source>jdbc/gt</jta-data-source>
    <non-jta-data-source>jdbc/gt</non-jta-data-source>
    <properties>
        <property name="hibernate.transaction.manager_lookup_class"
            value="org.hibernate.transaction.JBossTransactionManagerLookup" />
        <property name="hibernate.dialect" value="org.hibernate.dialect.Oracle8iDialect" />
        <property name="hibernate.show_sql" value="false" />
        <property name="hibernate.cache.use_second_level_cache"
            value="true" />
        <property name="hibernate.cache.use_query_cache" value="true" />
        <property name="hibernate.format_sql" value="false" />
        <property name="hibernate.hbm2ddl.auto" value="create" />
    </properties>
</persistence-unit> 

Clientebean:

package Beans;

import java.io.Serializable;
import java.util.List;

import javax.faces.bean.ManagedBean;
import javax.persistence.EntityManager;

import com.bd.Cliente;
import com.bd.JpaUtil;

import Repository.ClienteRepository;

@ManagedBean(name = "ClienteBean")
public class ClienteBean implements Serializable {

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

    private Cliente cliente = new Cliente();
    private static List<Cliente> listaClientes;

    public void insertPedido() {
        System.out.println(cliente);
        EntityManager manager = JpaUtil.getEntityManager();
        ClienteRepository.insertCliente(cliente, manager);
    }

    public void updatePedido() {
        ClienteRepository.updateCliente(cliente);
    }

    public void delete() {
        ClienteRepository.delete(cliente);
    }

    public void todosNomes() {
        listaClientes = ClienteRepository.todosNomes();
    }

    public void todosClientes() {
        listaClientes = ClienteRepository.todos();
    }

    public void consutaPorId(int id) {
        cliente = ClienteRepository.consultaCliente(id);
    }

    public static void consultaPorCpf(Cliente cliente) {
        listaClientes = ClienteRepository.consultaPorCpf(cliente);
    }

    public static void consultaPorNome(Cliente cliente) {
        listaClientes = ClienteRepository.consultaPorNome(cliente);
    }



public Cliente getCliente() {
        return cliente;
    }

    public List<Cliente> getListaClientes() {
        return listaClientes;
    }

}

Clienterepository:

package Repository;

import java.io.Serializable;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.EntityTransaction;
import javax.persistence.Query;
import javax.persistence.TypedQuery;
import javax.transaction.TransactionScoped;
import javax.transaction.Transactional;
import javax.transaction.Transactional.TxType;

import com.bd.Cliente;

public class ClienteRepository implements Serializable {

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

    private static EntityManager manager;
    static EntityTransaction trx;

    public static void setManager(EntityManager manager) {
        ClienteRepository.manager = manager;
        trx = manager.getTransaction();
    }

    public static void insertCliente(Cliente cliente) {

        trx.begin();

        manager.persist(cliente);

        trx.commit();
    }

    public static void updateCliente(Cliente cliente) {

        Cliente resultado = new Cliente();

        trx.begin();

        if (cliente == null)
            return;

        resultado = consultaCliente(cliente.getId());
        if (!cliente.getCpf().equals(""))
            resultado.setCpf(cliente.getCpf());
        if (!cliente.getNome().equals(""))
            resultado.setNome(cliente.getNome());
        if (!cliente.getRg().equals(""))
            resultado.setRg(cliente.getNome());

        manager.merge(resultado);

        trx.commit();

    }

    public static void delete(Cliente cliente) {

        cliente = consultaCliente(cliente.getId());

        trx.begin();

        manager.remove(cliente);

        trx.commit();
    }

    public static Cliente consultaCliente(int id) {
        Cliente resultado = manager.find(Cliente.class, id);
        return resultado;
    }

    public static List<Cliente> consultaPorCpf(Cliente cliente) {
        Query query = manager.createNamedQuery("Cliente.buscaPorCpf");
        query.setParameter("cpf", cliente.getCpf());
        return query.getResultList();
    }

    public static List<Cliente> todosNomes() {
        Query query = manager.createNamedQuery("Cliente.todosNomes");
        return query.getResultList();
    }

    public static List<Cliente> todos() {
        Query query = manager.createNamedQuery("Cliente.todos");
        return query.getResultList();
    }

    public static List<Cliente> consultaPorNome(Cliente cliente) {
        Query query = manager.createNamedQuery("Cliente.buscaPorNome");
        query.setParameter("nome", cliente.getNome());
        return query.getResultList();
    }

}

Jpautil:

package com.bd;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;

public class JpaUtil {
    private static EntityManagerFactory factory;
    static {
        factory = Persistence.createEntityManagerFactory("MC");
    }

    public static EntityManager getEntityManager() {
        return factory.createEntityManager();
    }
}

From now on I appreciate any help!

  • Your Managedbean has no setCliente(Cliente cliente)?

2 answers

1

Thank you for the reply!

I added the parameter to my persistence and the error changed:

Caused by: java.lang.ClassNotFoundException: org.hibernate.cache.EhCacheProvider from BaseClassLoader@c3567a8{VFSClassLoaderPolicy@3b1f875f{name=vfsfile:/C:/jboss-5.1.0.GA/server/default/conf/jboss-service.xml domain=ClassLoaderDomain@2059d6e6{name=DefaultDomain parentPolicy=BEFORE parent=org.jboss.bootstrap.NoAnnotationURLClassLoader@3852865d} roots=[MemoryContextHandler@1054138533[path= context=vfsmemory://5c4o1k2u-5tiakz-j586yqxg-1-j586ysxn-7 real=vfsmemory://5c4o1k2u-5tiakz-j586yqxg-1-j586ysxn-7], DelegatingHandler@1593530820[path=ojdbc14.jar context=file:/C:/jboss-5.1.0.GA/server/default/lib/ real=file:/C:/jboss-5.1.0.GA/server/default/lib/ojdbc14.jar], DelegatingHandler@2035576212[path=antlr.jar context=file:/C:/jboss-5.1.0.GA/common/lib/ real=file:/C:/jboss-5.1.0.GA/common/lib/antlr.jar], **...**  org.jboss.wsf.common.concurrent, org.jboss.ejb.plugins.cmp.jdbc.bridge, javax.ejb] <IMPORT-ALL>NON_EMPTY}}
at org.jboss.classloader.spi.base.BaseClassLoader.loadClass(BaseClassLoader.java:448)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Unknown Source)
at org.hibernate.util.ReflectHelper.classForName(ReflectHelper.java:123)
at org.hibernate.cache.impl.bridge.RegionFactoryCacheProviderBridge.<init>(RegionFactoryCacheProviderBridge.java:63)
... 64 more

DEPLOYMENTS IN ERROR:
  Deployment "persistence.unit:unitName=#MC" is in error due to the following reason(s): java.lang.ClassNotFoundException: org.hibernate.cache.EhCacheProvider from BaseClassLoader@c3567a8{VFSClassLoaderPolicy@3b1f875f{name=vfsfile:/C:/jboss-5.1.0.GA/server/default/conf/jboss-service.xml **...**
    at org.jboss.deployers.plugins.deployers.DeployersImpl.checkComplete(DeployersImpl.java:993)
    at org.jboss.deployers.plugins.deployers.DeployersImpl.checkComplete(DeployersImpl.java:939)
    at org.jboss.deployers.plugins.main.MainDeployerImpl.checkComplete(MainDeployerImpl.java:873)
    at org.jboss.system.server.profileservice.repository.MainDeployerAdapter.checkComplete(MainDeployerAdapter.java:128)
    at org.jboss.system.server.profileservice.hotdeploy.HDScanner.scan(HDScanner.java:369)
    at org.jboss.system.server.profileservice.hotdeploy.HDScanner.run(HDScanner.java:255)
    at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source)
    at java.util.concurrent.FutureTask.runAndReset(Unknown Source)
    at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$301(Unknown Source)
    at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)

I also put the setCliente(client) in Managedbean that I had forgotten.

1

Add the following parameter to your persistence.xml

<property name="hibernate.cache.provider_class" value="org.hibernate.cache.EhCacheProvider"/>

This will force Hibernate to use Ehcache

Browser other questions tagged

You are not signed in. Login or sign up in order to post.