Olá galera, estou começando agora a usar hibernate p/ fazer persistencia no meu banco.
A questão é a seguinte, eu fiz um projeto de teste usando hibernate, enquanto eu rodo o projeto no eclipse(usando uma classe de teste), o hibernate funciona perfeitamente, porém, quando eu ligo o meu servidor que eu tento acessar minha página, ele gera um erro de ClassNotFoundException.
Vamos ao código:
Eu tenho uma classe HibernateUtil que irá me a sessão do hibernate
[code]package br.com.perfaco.cliente;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;
public class HibernateUtil {
private static SessionFactory factory;
static{
AnnotationConfiguration cfg = new AnnotationConfiguration().configure();
cfg.addAnnotatedClass(Cliente.class);
factory = cfg.buildSessionFactory();
}
public Session getSession(){
return factory.openSession();
}
}
[/code]
Eu tenho a minha classe ClienteDAO. Seu construtor recebe uma Session como parametro.
[code]package br.com.perfaco.cliente;
import java.util.List;
import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.Session;
public class ClienteDAO {
private Session session;
public ClienteDAO(Session session){
this.session=session;
}
public void salva(Cliente c){
this.session.save(c);
}
public void remove(Cliente c){
this.session.delete(c);
}
public Cliente procura(Long id){
return (Cliente) this.session.load(Cliente.class, id);
}
public void atualiza(Cliente c){
this.session.update(c);
}
public List<Cliente> listaTudo(){
this.session.beginTransaction();
Criteria criterio = this.session.createCriteria(Cliente.class);
return criterio.list();
}
public List<Cliente> listaId(Long id){
Query query = this.session.createQuery("from Cliente where id = :id");
query.setLong("id", id);
return query.list();
}
}
[/code]
E a Action ClienteAction:
[code]package br.com.perfaco.action;
import java.util.List;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.hibernate.Session;
import br.com.perfaco.cliente.Cliente;
import br.com.perfaco.cliente.ClienteDAO;
import br.com.perfaco.cliente.HibernateUtil;
public class ClienteAction {
Session session = new HibernateUtil().getSession();
private Cliente cliente;
private List lista;
@Action(value="cadastrarCliente", results={
@Result(location="clientes-cadastrados.jsp", name="ok")}
)
public String salvarEndereco(){
new ClienteDAO(session).salva(cliente);
return "ok";
}
public Cliente getCliente() {
return cliente;
}
public void setCliente(Cliente cliente) {
this.cliente = cliente;
}
@Action(value="listarClientes", results={
@Result(location="clientes-cadastrados.jsp", name="ok")}
)
public String listaClientes(){
session.beginTransaction();
lista=new ClienteDAO(session).listaTudo();
session.getTransaction().commit();
session.close();
return"ok";
}
public List<Cliente> getList(){
return lista;
}
}
[/code]
e por fim, o erro:
OBS: o erro ocorre somente quando eu tento acessar a página http://localhost:8080/Perfaco/listarClientes, a de cadastrarCliente eu ainda não testei.
[quote]02/12/2011 13:18:22 org.apache.catalina.core.AprLifecycleListener init
INFO: The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: C:\Program Files\Java\jre6\bin;.;C:\Windows\Sun\Java\bin;C:\Windows\system32;C:\Windows;C:\Program Files\Common Files\Microsoft Shared\Windows Live;C:\Program Files (x86)\Common Files\Microsoft Shared\Windows Live;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0;C:\Program Files (x86)\Common Files\Roxio Shared\DLLShared;C:\Program Files (x86)\Common Files\Roxio Shared\12.0\DLLShared;C:\Program Files (x86)\Windows Live\Shared;C:\Program Files (x86)\ATI Technologies\ATI.ACE\Core-Static;C:\Program Files\MySQL\MySQL Server 5.5\bin
02/12/2011 13:18:22 org.apache.tomcat.util.digester.SetPropertiesRule begin
AVISO: [SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting property ‘source’ to ‘org.eclipse.jst.jee.server:perfaco’ did not find a matching property.
02/12/2011 13:18:22 org.apache.tomcat.util.digester.SetPropertiesRule begin
AVISO: [SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting property ‘source’ to ‘org.eclipse.jst.jee.server:fj21-tarefas’ did not find a matching property.
02/12/2011 13:18:22 org.apache.coyote.http11.Http11Protocol init
INFO: Initializing Coyote HTTP/1.1 on http-8080
02/12/2011 13:18:22 org.apache.catalina.startup.Catalina load
INFO: Initialization processed in 426 ms
02/12/2011 13:18:22 org.apache.catalina.core.StandardService start
INFO: Starting service Catalina
02/12/2011 13:18:22 org.apache.catalina.core.StandardEngine start
INFO: Starting Servlet Engine: Apache Tomcat/6.0.32
02/12/2011 13:18:23 com.opensymphony.xwork2.util.logging.commons.CommonsLogger info
INFO: Parsing configuration file [struts-default.xml]
02/12/2011 13:18:23 com.opensymphony.xwork2.util.logging.commons.CommonsLogger info
INFO: Parsing configuration file [struts-plugin.xml]
02/12/2011 13:18:23 com.opensymphony.xwork2.util.logging.commons.CommonsLogger info
INFO: Unable to locate configuration files of the name struts.xml, skipping
02/12/2011 13:18:23 com.opensymphony.xwork2.util.logging.commons.CommonsLogger info
INFO: Parsing configuration file [struts.xml]
02/12/2011 13:18:23 com.opensymphony.xwork2.util.logging.commons.CommonsLogger info
INFO: Parsing configuration file [struts-default.xml]
02/12/2011 13:18:23 com.opensymphony.xwork2.util.logging.commons.CommonsLogger info
INFO: Parsing configuration file [struts-plugin.xml]
02/12/2011 13:18:23 com.opensymphony.xwork2.util.logging.commons.CommonsLogger info
INFO: Parsing configuration file [struts.xml]
02/12/2011 13:18:24 org.apache.catalina.startup.HostConfig deployDescriptor
INFO: Deploying configuration descriptor host-manager.xml
02/12/2011 13:18:24 org.apache.catalina.startup.HostConfig deployDescriptor
INFO: Deploying configuration descriptor manager.xml
02/12/2011 13:18:24 org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deploying web application directory docs
02/12/2011 13:18:24 org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deploying web application directory examples
02/12/2011 13:18:24 org.apache.catalina.core.ApplicationContext log
INFO: ContextListener: contextInitialized()
02/12/2011 13:18:24 org.apache.catalina.core.ApplicationContext log
INFO: SessionListener: contextInitialized()
02/12/2011 13:18:24 org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deploying web application directory ROOT
02/12/2011 13:18:24 org.apache.coyote.http11.Http11Protocol start
INFO: Starting Coyote HTTP/1.1 on http-8080
02/12/2011 13:18:24 org.apache.jk.common.ChannelSocket init
INFO: JK: ajp13 listening on /0.0.0.0:8009
02/12/2011 13:18:24 org.apache.jk.server.JkMain start
INFO: Jk running ID=0 time=0/16 config=null
02/12/2011 13:18:24 org.apache.catalina.startup.Catalina start
INFO: Server startup in 1682 ms
02/12/2011 13:18:37 org.apache.catalina.core.StandardWrapperValve invoke
GRAVE: Servlet.service() for servlet default threw exception
java.lang.ClassNotFoundException: org.hibernate.Session
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1680)
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1526)
at br.com.perfaco.action.ClienteAction.(ClienteAction.java:14)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at java.lang.Class.newInstance0(Unknown Source)
at java.lang.Class.newInstance(Unknown Source)
at com.opensymphony.xwork2.ObjectFactory.buildBean(ObjectFactory.java:119)
at com.opensymphony.xwork2.ObjectFactory.buildBean(ObjectFactory.java:150)
at com.opensymphony.xwork2.ObjectFactory.buildBean(ObjectFactory.java:139)
at com.opensymphony.xwork2.ObjectFactory.buildAction(ObjectFactory.java:109)
at com.opensymphony.xwork2.DefaultActionInvocation.createAction(DefaultActionInvocation.java:288)
at com.opensymphony.xwork2.DefaultActionInvocation.init(DefaultActionInvocation.java:388)
at com.opensymphony.xwork2.DefaultActionProxy.prepare(DefaultActionProxy.java:187)
at org.apache.struts2.impl.StrutsActionProxy.prepare(StrutsActionProxy.java:61)
at org.apache.struts2.impl.StrutsActionProxyFactory.createActionProxy(StrutsActionProxyFactory.java:39)
at com.opensymphony.xwork2.DefaultActionProxyFactory.createActionProxy(DefaultActionProxyFactory.java:47)
at org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:478)
at org.apache.struts2.dispatcher.ng.ExecuteOperations.executeAction(ExecuteOperations.java:77)
at org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter.doFilter(StrutsPrepareAndExecuteFilter.java:91)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:859)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:588)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
at java.lang.Thread.run(Unknown Source)
02/12/2011 13:29:35 org.apache.catalina.core.StandardWrapperValve invoke
GRAVE: Servlet.service() for servlet default threw exception
java.lang.ClassNotFoundException: org.hibernate.Session
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1680)
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1526)
at br.com.perfaco.action.ClienteAction.(ClienteAction.java:14)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at java.lang.Class.newInstance0(Unknown Source)
at java.lang.Class.newInstance(Unknown Source)
at com.opensymphony.xwork2.ObjectFactory.buildBean(ObjectFactory.java:119)
at com.opensymphony.xwork2.ObjectFactory.buildBean(ObjectFactory.java:150)
at com.opensymphony.xwork2.ObjectFactory.buildBean(ObjectFactory.java:139)
at com.opensymphony.xwork2.ObjectFactory.buildAction(ObjectFactory.java:109)
at com.opensymphony.xwork2.DefaultActionInvocation.createAction(DefaultActionInvocation.java:288)
at com.opensymphony.xwork2.DefaultActionInvocation.init(DefaultActionInvocation.java:388)
at com.opensymphony.xwork2.DefaultActionProxy.prepare(DefaultActionProxy.java:187)
at org.apache.struts2.impl.StrutsActionProxy.prepare(StrutsActionProxy.java:61)
at org.apache.struts2.impl.StrutsActionProxyFactory.createActionProxy(StrutsActionProxyFactory.java:39)
at com.opensymphony.xwork2.DefaultActionProxyFactory.createActionProxy(DefaultActionProxyFactory.java:47)
at org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:478)
at org.apache.struts2.dispatcher.ng.ExecuteOperations.executeAction(ExecuteOperations.java:77)
at org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter.doFilter(StrutsPrepareAndExecuteFilter.java:91)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:859)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:588)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
at java.lang.Thread.run(Unknown Source)
[/quote]
O código da minha classe que eu testei os métodos do ClienteDAO():
[code]package br.com.perfaco.cliente;
import java.util.List;
import org.hibernate.Session;
public class TesteCliente {
public static void main(String args[]){
Session session = new HibernateUtil().getSession();
ClienteDAO dao = new ClienteDAO(session);
/* Cliente c = new Cliente();
c.setNome(“MARCOS”);
c.setEndereco(“SHA CJ 4 CH 72”);
c.setTelefone(“34011956”);*/
session.beginTransaction();
List<Cliente>lista=dao.listaId((long)2);
for(Cliente c: lista){
System.out.println("Nome: "+c.getNome());
System.out.println("Endereço: "+c.getEndereco());
System.out.println("Telefone "+c.getTelefone());
System.out.println("ID: "+c.getId() );
}
session.getTransaction().commit();
session.close();
}
}
[/code]
Bom, é isso ai galera, conto com a ajuda de vcs mais uma vez.
Obrigado