Estou tentando gravar meus dados no banco de Dados com hibernate e JPA e nao estou conseguindo ter sucesso. quando uso no modo console os dados retornao com sucesso, mas quando rodo na web nao da certo.
erro abaixo.
type Exception report
message java.lang.NoClassDefFoundError: org/hibernate/service/ServiceRegistry
description The server encountered an internal error that prevented it from fulfilling this request.
exception
javax.servlet.ServletException: java.lang.NoClassDefFoundError: org/hibernate/service/ServiceRegistry
javax.faces.webapp.FacesServlet.service(FacesServlet.java:659)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
root cause
javax.faces.el.EvaluationException: java.lang.NoClassDefFoundError: org/hibernate/service/ServiceRegistry
javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:101)
com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:102)
javax.faces.component.UICommand.broadcast(UICommand.java:315)
javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:790)
javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1282)
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:198)
javax.faces.webapp.FacesServlet.service(FacesServlet.java:646)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
root cause
java.lang.NoClassDefFoundError: org/hibernate/service/ServiceRegistry
view.MeuClienteBean.gravar(MeuClienteBean.java:36)
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:279)
org.apache.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:273)
com.sun.faces.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:105)
javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:87)
com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:102)
javax.faces.component.UICommand.broadcast(UICommand.java:315)
javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:790)
javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1282)
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:198)
javax.faces.webapp.FacesServlet.service(FacesServlet.java:646)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
root cause
java.lang.ClassNotFoundException: org.hibernate.service.ServiceRegistry
org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1892)
org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1735)
view.MeuClienteBean.gravar(MeuClienteBean.java:36)
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:279)
org.apache.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:273)
com.sun.faces.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:105)
javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:87)
com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:102)
javax.faces.component.UICommand.broadcast(UICommand.java:315)
javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:790)
javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1282)
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:198)
javax.faces.webapp.FacesServlet.service(FacesServlet.java:646)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
note The full stack trace of the root cause is available in the Apache Tomcat/7.0.75 logs.
//**********************
minha class bean
package view;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.context.FacesContext;
import javax.faces.view.ViewScoped;
import org.hibernate.Session;
import org.hibernate.criterion.Order;
import modelo.MeuCliente;
import modelo.MinhaPessoa;
import util.HibernateUtil;
@SuppressWarnings("serial")
@ManagedBean
@ViewScoped
public class MeuClienteBean implements Serializable {
MinhaPessoa pessoa = new MinhaPessoa();
public MinhaPessoa getPessoa() {
return pessoa;
}
MeuCliente cliente = new MeuCliente();
public MeuCliente getCliente() {
return cliente;
}
public void gravar() {
Session session = HibernateUtil.getSession();
org.hibernate.Transaction trx = session.beginTransaction();
session.merge(this.minhaPessoa);
trx.commit();
session.close();
/*
* System.out.println("Nome:" + cliente.getNome());
* System.out.println("CPF: " + cliente.getCpf());
* System.out.println("Endereço: " + cliente.getEndereco());
* System.out.println("Cidade: " + cliente.getCidade());
* System.out.println("Sexo: " + cliente.getSexo());
*/
String msg = "Cadastroi Realizado com sucesso";
FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, msg, msg));
}
////// PESSOA ////
private List<MinhaPessoa> minhaPessoa = new ArrayList<MinhaPessoa>();
public List<MinhaPessoa> getMinhaPessoa() {
return minhaPessoa;
}
// Buscar a pessoa Dinamicamento no banco de Dados de forma automatica
@SuppressWarnings("unchecked")
// @PostConstruct
public void init() {
Session session = MeuHibernateUtil.getSeesSession();
this.minhaPessoa = session.createCriteria(MinhaPessoa.class).addOrder(Order.asc("nome")).list();
session.close();
}
}
/**********************************
minha class Pessoa.
package modelo;
import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table
public class MinhaPessoa implements Serializable {
private Integer codigo;
private String nome;
public MinhaPessoa() {
// TODO Auto-generated constructor stub
}
public MinhaPessoa( Integer codigo, String nome) {
this.codigo = codigo;
this.nome = nome;
}
@Id
@GeneratedValue
@Column(name ="id")
public Integer getCodigo() {
return codigo;
}
public void setCodigo(Integer codigo) {
this.codigo = codigo;
}
@Column
public String getNome() {
return nome;
}
public void setNome(String nome) {
this.nome = nome;
}
@Override
public int hashCode(){
final int prime = 31;
int result = 1;
result = prime * result + ((codigo == null)? 0 : codigo.hashCode());
return result;
}
@Override
public boolean equals(Object obj){
if(this == obj)
return true;
if(obj==null)
return false;
if(getClass() !=obj.getClass())
return false;
MinhaPessoa other = (MinhaPessoa)obj;
if(codigo == null){
if(other.codigo != null)
return false;
}else if(!codigo.equals(other.codigo))
return false;
return true;
}
}
/************************************
minha sessão para conexão
package util;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;
public class HibernateUtil {
private static final SessionFactory sessionFactory;
static {
try {
Configuration configuration = new Configuration();
configuration.configure();
ServiceRegistry serviceRegistry = new ServiceRegistryBuilder()
.applySettings(configuration.getProperties()).getBootstrapServiceRegistry();
sessionFactory = configuration.buildSessionFactory(serviceRegistry);
} catch (Throwable e) {
throw new ExceptionInInitializerError(e);
}
}
public static Session getSession(){
return sessionFactory.openSession();
}
}
//****************
configuaração xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost/sistema_maia</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">192522</property>
<property name="dialect">org.hibernate.dialect.MySQL5Dialect</property>
<property name="show_sql">true</property>
<property name="format_sql">true</property>
<mapping class="modelo.MinhaPessoa"/>
</session-factory>
</hibernate-configuration>
//***********
meu xhtml
<?xml version="1.0" encoding="ISO-8859-1" ?>
<!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>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<title>Cliente</title>
</h:head>
<h:body>
<h:form>
<h1>Cadastro de Clientes</h1>
<h:panelGrid id="grid" columns="4">
<h:outputLabel value="Nome:" />
<h:inputText size="40" value="#{meuClienteBean.pessoa.nome}" />
</h:panelGrid>
<h:panelGrid columns="2">
<h:commandButton value="Gravar" action="#{meuClienteBean.gravar}" />
<h:commandButton value="Limpar" />
</h:panelGrid>
</h:form>
</h:body>
</html>
/*/*/*
alguem sabe onde estou errando...
minha class teste. da certo.
import java.util.List;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;
import model.Pessoa;
import util.HibernateUtil;
public class Testes {
public static void main(String[] args) {
Session session = HibernateUtil.getSession();
List<Pessoa> pessoas = session.createCriteria(Pessoa.class).list();
for (Pessoa p : pessoas) {
System.out.println(p.getCodigo() + " - " + p.getNome());
}
session.close();
}
}
...]
Hibernate:
select
this_.id as id1_0_0_,
this_.nome as nome2_0_0_
from
Pessoa this_
1 - Lucio Dowglas Rodrigues Maia
2 - Kayron Colares Maia