title: new to spring, not able to delete user using hibernate
body: <p>I am new to spring, was working on an example. The example shows how to add data to table using hibernate and list it also, I tried to add extra delete option but its updating record instead. I will post complete code below. ( 2 ) I develop simple web apps using jsp and servlets and lots of jquery ajax, I want to use ajax also with spring, will I be able to code the same way or its something different which i have to learn. To be frank I am scared after seeing the spring pattern.. :).</p>
  
  <p>dispatcher-servlet.xml</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;beans xmlns="http://www.springframework.org/schema/beans"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xmlns:p="http://www.springframework.org/schema/p"
         xmlns:aop="http://www.springframework.org/schema/aop"
         xmlns:tx="http://www.springframework.org/schema/tx"
         xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
         http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
         http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"&gt;
  
      &lt;bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping"/&gt;
  
      &lt;!--
      Most controllers will use the ControllerClassNameHandlerMapping above, but
      for the index controller we are using ParameterizableViewController, so we must
      define an explicit mapping for it.
      --&gt;
      &lt;bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"&gt;
          &lt;property name="mappings"&gt;
              &lt;props&gt;
                  &lt;prop key="index.htm"&gt;indexController&lt;/prop&gt;
              &lt;/props&gt;
          &lt;/property&gt;
      &lt;/bean&gt;
  
      &lt;bean id="viewResolver"
            class="org.springframework.web.servlet.view.InternalResourceViewResolver"
            p:prefix="/WEB-INF/jsp/"
            p:suffix=".jsp" /&gt;
  
      &lt;bean id="myDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"&gt;
          &lt;property name="driverClassName" value="com.mysql.jdbc.Driver"/&gt;
          &lt;property name="url" value="jdbc:mysql://localhost:3306/production"/&gt;
          &lt;property name="username" value="tpsl_admin"/&gt;
          &lt;property name="password" value="admin"/&gt;
      &lt;/bean&gt;
  
      &lt;bean id="mySessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"&gt;
          &lt;property name="dataSource" ref="myDataSource" /&gt;
          &lt;property name="annotatedClasses"&gt;
              &lt;list&gt;
                  &lt;value&gt;com.org.domain.User&lt;/value&gt;
              &lt;/list&gt;
          &lt;/property&gt;
          &lt;property name="hibernateProperties"&gt;
              &lt;props&gt;
                  &lt;prop key="hibernate.dialect"&gt;org.hibernate.dialect.MySQLDialect&lt;/prop&gt;
                  &lt;prop key="hibernate.show_sql"&gt;true&lt;/prop&gt;
                  &lt;prop key="hibernate.hbm2ddl.auto"&gt;create&lt;/prop&gt;
              &lt;/props&gt;
          &lt;/property&gt;
      &lt;/bean&gt;
  
      &lt;bean id="myUserDAO" class="com.org.dao.UserDAOImpl"&gt;
          &lt;property name="sessionFactory" ref="mySessionFactory"/&gt;
      &lt;/bean&gt;
  
      &lt;bean name="/user/*.htm" class="com.org.web.UserController" &gt;
          &lt;property name="userDAO" ref="myUserDAO" /&gt;
      &lt;/bean&gt;
  
      &lt;!--
      The index controller.
      --&gt;
      &lt;bean name="indexController" class="org.springframework.web.servlet.mvc.ParameterizableViewController"
            p:viewName="index" /&gt;
  
  &lt;/beans&gt;
  </code></pre>
  
  <p>web.xml</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"&gt;
      &lt;context-param&gt;
          &lt;param-name&gt;contextConfigLocation&lt;/param-name&gt;
          &lt;param-value&gt;/WEB-INF/applicationContext.xml&lt;/param-value&gt;
      &lt;/context-param&gt;
      &lt;listener&gt;
          &lt;listener-class&gt;org.springframework.web.context.ContextLoaderListener&lt;/listener-class&gt;
      &lt;/listener&gt;
      &lt;servlet&gt;
          &lt;servlet-name&gt;dispatcher&lt;/servlet-name&gt;
          &lt;servlet-class&gt;org.springframework.web.servlet.DispatcherServlet&lt;/servlet-class&gt;
          &lt;load-on-startup&gt;2&lt;/load-on-startup&gt;
      &lt;/servlet&gt;
      &lt;servlet-mapping&gt;
          &lt;servlet-name&gt;dispatcher&lt;/servlet-name&gt;
          &lt;url-pattern&gt;*.htm&lt;/url-pattern&gt;
      &lt;/servlet-mapping&gt;
      &lt;session-config&gt;
          &lt;session-timeout&gt;
              30
          &lt;/session-timeout&gt;
      &lt;/session-config&gt;
      &lt;welcome-file-list&gt;
          &lt;welcome-file&gt;redirect.jsp&lt;/welcome-file&gt;
      &lt;/welcome-file-list&gt;
  &lt;/web-app&gt;
  </code></pre>
  
  <p>UserDAO.java</p>
  
  <pre><code>package com.org.dao;
  import com.org.domain.User;
  import java.util.List;
  
  public interface UserDAO {
  
      public void saveUser(User user) ;
      public List&lt;User&gt; listUser() ;
          public void deleteUser(User user) ;
  }
  </code></pre>
  
  <p>UserDAOImpl.java</p>
  
  <pre><code>package com.org.dao;
  import com.org.domain.User;
  import java.util.List;
  import org.hibernate.SessionFactory;
  import org.springframework.orm.hibernate3.HibernateTemplate;
  
  public class UserDAOImpl implements UserDAO {
  
      private HibernateTemplate hibernateTemplate;
  
      public void setSessionFactory(SessionFactory sessionFactory) {
          this.hibernateTemplate = new HibernateTemplate(sessionFactory);
      }
  
      @Override
      public void saveUser(User user) {
          hibernateTemplate.saveOrUpdate(user);
      }
  
      @Override
      @SuppressWarnings("unchecked")
      public List&lt;User&gt; listUser() {
          return hibernateTemplate.find("from User");
      }
  
          @Override
      public void deleteUser(User user) {
          hibernateTemplate.delete(user);
      }
  }
  </code></pre>
  
  <p>User.java</p>
  
  <pre><code>package com.org.domain;
  
  import javax.persistence.Column;
  import javax.persistence.Entity;
  import javax.persistence.GeneratedValue;
  import javax.persistence.Id;
  import javax.persistence.Table;
  
  @Entity
  @Table(name="USER")
  public class User {
  
      private Long id;
      private String name;
      private String password;
      private String gender;
      private String country;
      private String aboutYou;
      private String[] community;
      private Boolean mailingList;
  
      @Id
      @GeneratedValue
      @Column(name="USER_ID")
      public Long getId() {
          return id;
      }
      public void setId(Long id) {
          this.id = id;
      }
  
      @Column(name="USER_NAME")
      public String getName() {
          return name;
      }
      public void setName(String name) {
          this.name = name;
      }
  
      @Column(name="USER_PASSWORD")
      public String getPassword() {
          return password;
      }
      public void setPassword(String password) {
          this.password = password;
      }
  
      @Column(name="USER_GENDER")
      public String getGender() {
          return gender;
      }
      public void setGender(String gender) {
          this.gender = gender;
      }
  
      @Column(name="USER_COUNTRY")
      public String getCountry() {
          return country;
      }
      public void setCountry(String country) {
          this.country = country;
      }
  
      @Column(name="USER_ABOUT_YOU")
      public String getAboutYou() {
          return aboutYou;
      }
      public void setAboutYou(String aboutYou) {
          this.aboutYou = aboutYou;
      }
  
      @Column(name="USER_COMMUNITY")
      public String[] getCommunity() {
          return community;
      }
      public void setCommunity(String[] community) {
          this.community = community;
      }
  
      @Column(name="USER_MAILING_LIST")
      public Boolean getMailingList() {
          return mailingList;
      }
      public void setMailingList(Boolean mailingList) {
          this.mailingList = mailingList;
      }
  
  }
  </code></pre>
  
  <p>UserController.java</p>
  
  <pre><code>package com.org.web;
  import com.org.dao.UserDAO;
  import com.org.domain.User;
  import javax.servlet.http.HttpServletRequest;
  import javax.servlet.http.HttpServletResponse;
  import org.springframework.transaction.annotation.Transactional;
  import org.springframework.ui.ModelMap;
  import org.springframework.web.bind.annotation.RequestMapping;
  import org.springframework.web.bind.annotation.RequestMethod;
  import org.springframework.web.servlet.ModelAndView;
  import org.springframework.web.servlet.mvc.multiaction.MultiActionController;
  
  public class UserController extends MultiActionController {
  
      private UserDAO userDAO;
  
      public void setUserDAO(UserDAO userDAO) {
          this.userDAO = userDAO;
      }
          @RequestMapping(params = "add", method = RequestMethod.POST)
      public ModelAndView add(HttpServletRequest request,
              HttpServletResponse response, User user) throws Exception {
          userDAO.saveUser(user);
          return new ModelAndView("redirect:list.htm");
      }
          @RequestMapping(params = "delete", method = RequestMethod.POST)
          @Transactional
          public ModelAndView delete(HttpServletRequest request,
              HttpServletResponse response, User user) throws Exception {
          userDAO.deleteUser(user);
          return new ModelAndView("redirect:list.htm");
      }
  
      public ModelAndView list(HttpServletRequest request,
              HttpServletResponse response) throws Exception {
          ModelMap modelMap = new ModelMap();
          modelMap.addAttribute("userList", userDAO.listUser());
          modelMap.addAttribute("user", new User());
          return new ModelAndView("userForm", modelMap);
      }
  }
  </code></pre>
  
  <p>ok thats all with the code, the ide i am using is netbeans, sprinf ver is 3.1.1. Hibernate 3.0. Some one please show me where i need to change I am trying since 2 days with fail. Also explain my own code if possible I have many doubts.</p>
  
  <p>Thankyou</p>
  
  <p>userForm.jsp</p>
  
  <pre><code>&lt;%@ page language="java" contentType="text/html; charset=ISO-8859-1"
      pageEncoding="ISO-8859-1"%&gt;
  &lt;%@ taglib uri="http://www.springframework.org/tags/form" prefix="form"%&gt;
  &lt;%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%&gt;
  &lt;%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn"%&gt;
  &lt;html&gt;
  &lt;head&gt;
  &lt;meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"&gt;
  &lt;style type="text/css"&gt;
  .even {
      background-color: silver;
  }
  &lt;/style&gt;
  &lt;title&gt;Registration Page&lt;/title&gt;
  &lt;/head&gt;
  &lt;body&gt;
  
      &lt;form:form action="add.htm" commandName="user" method="post"&gt;
      &lt;table&gt;
  
          &lt;tr&gt;
              &lt;td&gt;User Name :&lt;/td&gt;
              &lt;td&gt;&lt;form:input path="name" /&gt;&lt;/td&gt;
          &lt;/tr&gt;
          &lt;tr&gt;
              &lt;td&gt;Password :&lt;/td&gt;
              &lt;td&gt;&lt;form:password path="password" /&gt;&lt;/td&gt;
          &lt;/tr&gt;
          &lt;tr&gt;
              &lt;td&gt;Gender :&lt;/td&gt;
              &lt;td&gt;&lt;form:radiobutton path="gender" value="M" label="M" /&gt; 
                              &lt;form:radiobutton path="gender" value="F" label="F" /&gt;&lt;/td&gt;
          &lt;/tr&gt;
          &lt;tr&gt;
              &lt;td&gt;Country :&lt;/td&gt;
              &lt;td&gt;&lt;form:select path="country"&gt;
                  &lt;form:option value="0" label="Select" /&gt;
                  &lt;form:option value="India" label="India" /&gt;
                  &lt;form:option value="USA" label="USA" /&gt;
                  &lt;form:option value="UK" label="UK" /&gt;
              &lt;/form:select&gt;&lt;/td&gt;
          &lt;/tr&gt;
          &lt;tr&gt;
              &lt;td&gt;About you :&lt;/td&gt;
              &lt;td&gt;&lt;form:textarea path="aboutYou" /&gt;&lt;/td&gt;
          &lt;/tr&gt;
          &lt;tr&gt;
              &lt;td&gt;Community :&lt;/td&gt;
              &lt;td&gt;&lt;form:checkbox path="community" value="Spring" label="Spring" /&gt; 
                              &lt;form:checkbox path="community" value="Hibernate" label="Hibernate" /&gt;
                              &lt;form:checkbox path="community" value="Struts" label="Struts" /&gt;
                          &lt;/td&gt;
          &lt;/tr&gt;
          &lt;tr&gt;
              &lt;td&gt;&lt;/td&gt;
              &lt;td&gt;&lt;form:checkbox path="mailingList" label="Would you like to join our mailinglist?" /&gt;&lt;/td&gt;
          &lt;/tr&gt;
          &lt;tr&gt;
              &lt;td colspan="2"&gt;&lt;input type="submit" name="add" value="Register"&gt;&lt;/td&gt;
                          &lt;td colspan="2"&gt;&lt;input type="button" name="delete" value="Delete"&gt;&lt;/td&gt;
          &lt;/tr&gt;
      &lt;/table&gt;
  &lt;/form:form&gt;
  &lt;c:if test="${fn:length(userList) &gt; 0}"&gt;
      &lt;table cellpadding="5"&gt;
          &lt;tr class="even"&gt;
                          &lt;th&gt;ID&lt;/th&gt;
              &lt;th&gt;Name&lt;/th&gt;
              &lt;th&gt;Gender&lt;/th&gt;
              &lt;th&gt;Country&lt;/th&gt;
              &lt;th&gt;About You&lt;/th&gt;
                          &lt;th&gt;Mailing List&lt;/th&gt;
                          &lt;th&gt;Community&lt;/th&gt;
          &lt;/tr&gt;
          &lt;c:forEach items="${userList}" var="user" varStatus="status"&gt;
              &lt;tr class="&lt;c:if test="${status.count % 2 == 0}"&gt;even&lt;/c:if&gt;"&gt;
                                  &lt;td&gt;${user.id}&lt;/td&gt;
                  &lt;td&gt;${user.name}&lt;/td&gt;
                  &lt;td&gt;${user.gender}&lt;/td&gt;
                  &lt;td&gt;${user.country}&lt;/td&gt;
                  &lt;td&gt;${user.aboutYou}&lt;/td&gt;
                                  &lt;td&gt;${user.mailingList}&lt;/td&gt;
                                  &lt;td&gt;${user.community}&lt;/td&gt;
              &lt;/tr&gt;
          &lt;/c:forEach&gt;
      &lt;/table&gt;
  &lt;/c:if&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>