5 Replies Latest reply on Nov 4, 2009 1:56 AM by pepelara

    Simple hibernate web app

    pepelara

      Hello,


      I am a newbe in Hibernate but I am learning quickly. But I have reached a point that is taking me a lot of time.

      I have developed a pretty simple web. It is just two JSP, index.jsp that redirects to user.jsp where I have all the code.

      This is user.jsp,

      <%@page import="com.examscam.dao.*,com.examscam.model.*,
      com.examscam.util.*,org.hibernate.*;" contentType="text/html;" %>
      
      <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
      
      <jsp:useBean class="com.examscam.model.User" id="user" scope="request"/>
      <jsp:setProperty name="user" property="*" />
      
      <%
      HibernateUtil.beginTransaction();
      UserDAO userDAO = new HibernateUserDAO();
      java.util.List<User> users = null;
      String command = request.getParameter("command");
      if (command != null) {
      if (command.equals("Create")) {
      userDAO.create(user);
      }
      if (command.equals("Update")) {
      userDAO.update(user);
      }
      if (command.equals("edit")) {
      user = userDAO.findByPrimaryKey(user.getId());
      request.setAttribute("user", user);
      }
      if (command.equals("delete")) {
      userDAO.delete(user);
      request.setAttribute("user", null);
      }
      if (command.equals("Fuzzy Search")) {
      users = userDAO.findByExample(user, true);
      }
      if (command.equals("Strict Search")) {
      users = userDAO.findByExample(user, false);
      }
      if (command.equals("Clear")) {
      request.setAttribute("user", null);
      }
      }
      if (users == null) {
      users = userDAO.findAll();
      }
      request.setAttribute("users", users);
      HibernateUtil.commitTransaction();
      %>
      
      <html>
      <body>
      <form action="user.jsp">
      <!-- Here are our four textfields -->
      <input type="text" size="7"
      name="id" value=""> Id
      <BR/>
      <input type="text" size="30"
      name="loginName" value=""> Name
      <BR/>
      <input type="text" size="30"
      name="password" value=""> Password
      <BR/>
      <input type="text" size="30"
      name="emailAddress" value=""> Email
      <BR/>
      <!-- Here are all of our buttons!!! -->
      <input type="submit" name="command" value="Strict Search">
      <input type="submit" name="command" value="Fuzzy Search">
      <input type="submit" name="command" value="Update">
      <input type="submit" name="command" value="Create">
      <input type="submit" name="command" value="Clear">
      <BR>
      <c:forEach items="" var="user">
      <c:url var="editurl" value="user.jsp" >
       <c:param name="command" value="edit" />
       <c:param name="id" value=""/>
      </c:url>
      <c:url var="deleteurl" value="user.jsp" >
       <c:param name="command" value="delete" />
       <c:param name="id" value=""/>
      </c:url>
       <a href="">edit</a>
       <a href="">delete</a>
       <c:out value="" />
       <c:out value="" />
       <c:out value="" />
      <BR/>
      </c:forEach>
      </form>
      </body>
      </html>
      



      The problem is that I am not building the web app correctly. For user.jsp I import a jar, hibernatedaoaccess.jar, where are the classes and interfaces I need,

      package com.examscam.dao;
      
      import java.util.List;
      import com.examscam.model.User;
      
      public interface UserDAO {
      
       public User create(User user);
       public boolean update(User user) ;
       public boolean delete(User user) ;
       public User findByPrimaryKey(Long primaryKey);
       public List<User> findByExample(User user, boolean fuzzy);
       public List<User> findAll();
      
      }
      


      package com.examscam.model;
      
      import javax.persistence.Entity;
      import javax.persistence.GeneratedValue;
      import javax.persistence.Id;
      import javax.persistence.Column;
      import javax.persistence.Table;
      import javax.persistence.Basic;
      import javax.persistence.Temporal;
      import javax.persistence.TemporalType;
      import javax.persistence.Transient;
      
      @Entity
      @Table (name="user", schema="examscam")
      public class User {
       private Long id;
       private String loginName;
       private String password;
       private String encryptedPassword;
       private String emailAddress;
       private Boolean verified;
       private java.util.Date lastAccessTime;
       private java.util.Calendar registrationDate;
      
       @Transient
       public String getEncryptedPassword() {
       return encryptedPassword;
       }
       public void setEncryptedPassword(String ep) {
       this.encryptedPassword = ep;
       }
      
       @Id
       @GeneratedValue
       @Column (name="id")
       public Long getId() {
       return id;
       }
       public void setId(Long id) {
       this.id = id;
       }
      
       @Column (name="login_name")
       public String getLoginName() {
       return loginName;
       }
       public void setLoginName(String loginName) {
       this.loginName = loginName;
       }
      
       @Column (name="password", nullable=false)
       public String getPassword() {
       return password;
       }
       public void setPassword(String password) {
       this.password = password;
       }
      
       @Column (name="emailAddress")
       public String getEmailAddress() {
       return emailAddress;
       }
      
       @Temporal(TemporalType.TIMESTAMP)
       public java.util.Date getLastAccessTime() {
       return lastAccessTime;
       }
      
       @Temporal(TemporalType.DATE)
       public java.util.Calendar getRegistrationDate() {
       return registrationDate;
       }
      
       @Basic
       public Boolean isVerified() {
       return verified;
       }
      
       public void setEmailAddress(String emailAddress) {
       this.emailAddress = emailAddress;
       }
      
       public void setLastAccessTime
       (java.util.Date lastAccessTime) {
       this.lastAccessTime = lastAccessTime;
       }
      
       public void setRegistrationDate
       (java.util.Calendar registrationDate){
       this.registrationDate = registrationDate;
       }
      
       public void setVerified(Boolean verified) {
       this.verified = verified;
       }
      
       public String toString() {
       return getId() + " : " +
       getLoginName() + " : " +
       getPassword() + " : " +
       getEmailAddress();
       }
      }
      


      package com.examscam.dao;
      
      import java.util.List;
      import org.hibernate.Criteria;
      import org.hibernate.Query;
      import org.hibernate.Session;
      import org.hibernate.criterion.Example;
      import org.hibernate.criterion.MatchMode;
      import com.examscam.model.User;
      
      
      public class HibernateUserDAO extends ExamScamDAO implements UserDAO {
      
       public User create(User user) {
       // TODO Auto-generated method stub
       if (user.getId() != null && user.getId() != 0) {
       user = null;
       }
       else {
       user.setLastAccessTime(new java.util.Date());
       user.setRegistrationDate
       (new java.util.GregorianCalendar());
       user.setVerified(false);
       super.save(user);
       }
       return user;
       }
      
       public boolean update(User user) {
       // TODO Auto-generated method stub
       boolean successFlag = true;
      
       try {
       if (user.getId() == null || user.getId() == 0) {
       successFlag = false;
       }else {
       super.save(user);
       }
       }
       catch (Throwable th) {
       successFlag = false;
       }
      
       return successFlag;
       }
      
       public boolean delete(User user) {
       // TODO Auto-generated method stub
       boolean successFlag = true;
      
       try {
       user.setPassword(" ");
       super.delete(user);
       } catch (Throwable th) {
       successFlag = false;
       }
      
       return successFlag;
       }
      
       @SuppressWarnings("unchecked")
       public List<User> findAll() {
       // TODO Auto-generated method stub
       String queryString = "from User";
       List<User> allUsers;
       Query queryResult =
       this.getSession().createQuery(queryString);
       allUsers = (List<User>) queryResult.list();
      
       return allUsers;
      
       }
      
       @SuppressWarnings("unchecked")
       public List<User> findByExample(User user, boolean fuzzy) {
       // TODO Auto-generated method stub
       List<User> users = null;
       Session session = this.getSession();
       Criteria criteria =
       session.createCriteria(User.class);
       Example example = Example.create(user);
       if (fuzzy) {
       example.enableLike(MatchMode.ANYWHERE);
       example.ignoreCase();
       example.excludeZeroes();
       }
       criteria.add(example);
       List<User> list = (List<User>) criteria.list();
       users = list;
       return users;
      
       }
      
       public User findByPrimaryKey(Long primaryKey) {
       // TODO Auto-generated method stub
       User user = (User)
       super.findByPrimaryKey(User.class, primaryKey);
      
       return user;
      
       }
      
      }
      


      The question is depending on where I put the jar, running the web app against JBoss 4.2.2 GA, it does not find the hibernate.cfg.xml file or the interface UserDao as well.

      And I can not get it works.

      Any idea?

      I am developing on Eclipse Ganymede RS1 that integrates the Data Base if needed and the apps server.

      Thanking in advance,
      Jose

        • 1. Re: Simple hibernate web app
          wolfgangknauf

          Hi,

          the JAR file has to be placed in "WEB-INF\lib" of your WAR file.
          But I cannot help you further with hibernate.cfg.xml files :-(.

          Best regards

          Wolfgang

          • 2. Re: Simple hibernate web app
            pepelara

            Hello Wolfgang,

            It is a wrong design of the project. I import a folder where there are some hibernate jars and the hibernate.cfg.xml file. The hibernatedaoaccess.jar file already imports that folder. But it does not exist in the jar. I mean the folder exists in the configuration of every project but when you jar those projects the folder does not include into them.

            That is the reason Jboss does not find the hibernate.cfg.xml file. I am better going to talk to the expert who wrote the tutorial for the right configuration of both hibernate and the projects

            I fell in it today, while writing this email.

            Thanks a lot,
            Jose

            • 3. Re: Simple hibernate web app
              pepelara

              Hi Wolfgang,

              I think you can help me.

              If a place the hibernate.cfg.xml file in a conf folder, how I have to
              do to JBoss reads the file?

              hibernate.cfg.xml is just a file where I set the database and the schema I am using. The server have to know about it do the Hibernate app works correctly

              Thanking in advance,
              Jose

              • 4. Re: Simple hibernate web app
                wolfgangknauf

                Hi Jose,

                better ask this in the hibernate forums: https://forum.hibernate.org/index.php
                I don't know how hibernate searches its config files, but hopefully in the forums someone can help.

                According to Hibernate docs, "The XML configuration file is by default expected to be in the root of your CLASSPATH". So, I think that it should be placed in "WEB-INF\classes", not in some dependent lib.

                Wolfgang

                • 5. Re: Simple hibernate web app
                  pepelara

                  Hi Wolfgang,

                  You are absolutely right.
                  That is what I did and the web app works ok.

                  Thanks a lot for your help.


                  Besta Regards,
                  Jose