[jboss-user] [EJB 3.0 Users] - Simple hibernate web app

pepelara do-not-reply at jboss.com
Wed Oct 28 23:36:36 EDT 2009


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

View the original post : http://www.jboss.org/index.html?module=bb&op=viewtopic&p=4262842#4262842

Reply to the post : http://www.jboss.org/index.html?module=bb&op=posting&mode=reply&p=4262842



More information about the jboss-user mailing list