-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: Error Hibernate + JSF
PostPosted: Sun Oct 04, 2009 6:55 pm 
Newbie

Joined: Sat Sep 12, 2009 11:21 am
Posts: 5
HI there,

I'm getting an error when I try to insert a record in my database. I'm using hibernate without annotations and it works just fine when i call the insert method from a java code. But when I use it from a jsf page I'm getting an error.
I have a class "Ambiente" that is used in the jsf page that contains just 2 atributes, "idAmbiente" and "descricao". I have another class that does the CRUD operations. From the jsf page I call the method "efetuaCadastro" that does the validations and calls the method "createAmbiente", from the CRUD class, that does the insert, but I'm getting the following error:

Code:
SEVERE: Servlet.service() for servlet Faces Servlet threw exception
java.lang.ClassNotFoundException: org.hibernate.Session
   at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1387)
   at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1233)
   at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:320)
   at acessoBanco.Ambiente.efetuaCadastro(Ambiente.java:48)
   at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
   at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
   at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
   at java.lang.reflect.Method.invoke(Method.java:597)
   at org.apache.myfaces.el.MethodBindingImpl.invoke(MethodBindingImpl.java:132)
   at org.apache.myfaces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:61)
   at javax.faces.component.UICommand.broadcast(UICommand.java:109)
   at javax.faces.component.UIViewRoot._broadcastForPhase(UIViewRoot.java:97)
   at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:171)
   at org.apache.myfaces.lifecycle.InvokeApplicationExecutor.execute(InvokeApplicationExecutor.java:32)
   at org.apache.myfaces.lifecycle.LifecycleImpl.executePhase(LifecycleImpl.java:95)
   at org.apache.myfaces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:70)
   at javax.faces.webapp.FacesServlet.service(FacesServlet.java:139)
   at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
   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:128)
   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:293)
   at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:849)
   at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:583)
   at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:454)
   at java.lang.Thread.run(Thread.java:619)


Here are the codes of my classes and the jsf page:

Code:
package acessoBanco;

import java.util.ArrayList;
import java.util.List;


public class Ambiente {

   private int idAmbiente;
   private String descricao;
   private List<String> errorMessages = new ArrayList<String>();

   public int getIdAmbiente(){return idAmbiente;}
   
   public void setIdAmbiente(int idAmbiente) {this.idAmbiente = idAmbiente;}
   
   public String getDescricao() {return descricao;}
   
   public void setDescricao(String descricao) {this.descricao = descricao;}
   
   public String getErrorMessages() {
      String messageList;
      if ((errorMessages == null) || (errorMessages.size() == 0)) {
         messageList = "";
      } else {
         messageList = "<FONT COLOR=RED><B><UL>\n";
         for(String message: errorMessages) {
            messageList = messageList + "<LI>" + message + "\n";
         }
         messageList = messageList + "</UL></B></FONT>\n";
      }
      return(messageList);
   }

   

   public String efetuaCadastro() {
      errorMessages = new ArrayList<String>();
      if (getIdAmbiente() == 0) {
         errorMessages.add("Preencha o id");''
      }
      if (getDescricao().equals("")) {
         errorMessages.add("Preencha a descrição");
      }
      if (errorMessages.size() > 0) {
         return(null);
      } else{
         CrudAmbiente.createAmbiente(getIdAmbiente(),getDescricao());
         return("success");
      }
   }
}

Code:
package acessoBanco;
import org.hibernate.*;
import org.hibernate.cfg.AnnotationConfiguration;


public class CrudAmbiente {

     public static void createAmbiente(int id, String desc){
          AnnotationConfiguration config = new AnnotationConfiguration();
          config.addAnnotatedClass(Ambiente.class);
          config.configure();
          SessionFactory factory = config.buildSessionFactory();
          Session session = factory.openSession();
          session.beginTransaction();
          System.out.println("creating ambiente");
         
          Ambiente ambiente = new Ambiente();
          ambiente.setIdAmbiente(id);
          ambiente.setDescricao(desc);
         
          session.save(ambiente);
          System.out.println("ambiente saved");
          session.getTransaction().commit();
          System.out.println("transaction successful!!!"); 
        }

     public static void retrieveAmbiente(){
        AnnotationConfiguration config = new AnnotationConfiguration();
        config.addAnnotatedClass(Ambiente.class);
        SessionFactory factory;
        factory = config.configure().buildSessionFactory();
        Session session = factory.getCurrentSession();
        session.beginTransaction();
       
        java.util.List allAmbientes;
        Query queryResult= session.createQuery("from Ambiente");
        allAmbientes = ((org.hibernate.Query) queryResult).list();
        for (int i = 0; i < allAmbientes.size(); i++) {
           Ambiente ambiente = (Ambiente) allAmbientes.get(i);
          System.out.println(ambiente.getDescricao() + " " + ambiente.getIdAmbiente());
        }
        session.getTransaction().commit();
     }
    
     public static void deleteAll() {
        AnnotationConfiguration config =
                                   new AnnotationConfiguration();
        config.addAnnotatedClass(Ambiente.class);
        SessionFactory factory;
        factory = config.configure().buildSessionFactory();
        Session session = factory.getCurrentSession();
        session.beginTransaction();
        java.util.List allAmbientes;
        System.out.println("Deleting all records...");
        Query queryResult = session.createQuery("from Ambiente");
        allAmbientes = queryResult.list();

        for (int i = 0; i < allAmbientes.size(); i++) {
           Ambiente ambiente = (Ambiente) allAmbientes.get(i);
          System.out.println(ambiente.getDescricao());
          session.delete(ambiente);
        }
        System.out.println("Database contents deleted...");
        session.getTransaction().commit();
      }
    
    
     public static Ambiente retrieveFromId(int idValue) {
        AnnotationConfiguration config = new AnnotationConfiguration();
        config.addAnnotatedClass(Ambiente.class);
        SessionFactory factory= config.configure().buildSessionFactory();
        Session session = factory.getCurrentSession();
        session.beginTransaction();
        String queryString = "from Ambiente where id = :id";
        Query query = session.createQuery(queryString);
        query.setInteger("id", idValue);
        Object queryResult = query.uniqueResult();
        Ambiente ambiente = (Ambiente)queryResult;session.getTransaction().commit();
        System.out.println(ambiente.getDescricao());
       
        return ambiente;
       }    

   
}


Code:
<%@ taglib uri="http://java.sun.com/jsf/core" prefix="f" %>
<%@ taglib uri="http://java.sun.com/jsf/html" prefix="h" %>
<f:view>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD><TITLE>Cadastro Ambiente</TITLE>
<LINK REL="STYLESHEET"
      HREF="./css/styles.css"
      TYPE="text/css">
</HEAD>
<BODY>
<CENTER>
<TABLE BORDER=5>
  <TR><TH CLASS="TITLE">SISTRACK - Cadastro de Ambiente</TH></TR>
</TABLE>

<h:form>
  <h:outputText value="#{ambiente.errorMessages}"
                escape="false"/>
  <TABLE>
  <TR>
    <TD>ID:
    <h:inputText value="#{ambiente.idAmbiente}"/></TD></TR>
  <TR>
    <TD>Descrição:
    <h:inputText value="#{ambiente.descricao}"/></TD></TR>
  <TR><TH>
      <h:commandButton value="Cadastrar"
                       action="#{ambiente.efetuaCadastro}"/></TH></TR>
  </TABLE>
</h:form>

<BR><BR><BR>

</CENTER></BODY></HTML>
</f:view>


If I comment the call "CrudAmbiente.createAmbiente(getIdAmbiente(),getDescricao()); " in the method efetuaCadastro it works ok and goes to the success page, but if its called the error above happens.

Does anybody have an ideia of what can be happening? Thanks.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.