title: Spring MVC and Hibernate Not Work
body: <p>Good Evening,
  I'm new in Spring and Hibernate and I can not fix this error:</p>
  
  <pre><code>GRAVE: Error configuring application listener of class org.springframework.web.context.ContextLoaderListener
  java.lang.ClassNotFoundException: org.springframework.web.context.ContextLoaderListener
      at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1305)
      at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1157)
      at org.apache.catalina.core.DefaultInstanceManager.loadClass(DefaultInstanceManager.java:520)
      at org.apache.catalina.core.DefaultInstanceManager.loadClassMaybePrivileged(DefaultInstanceManager.java:501)
      at org.apache.catalina.core.DefaultInstanceManager.newInstance(DefaultInstanceManager.java:120)
      at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4650)
      at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5166)
      at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
      at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1409)
      at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1399)
      at java.util.concurrent.FutureTask.run(Unknown Source)
      at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
      at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
      at java.lang.Thread.run(Unknown Source)
  
  mag 04, 2015 5:22:49 PM org.apache.catalina.core.StandardContext listenerStart
  GRAVE: Skipped installing application listeners due to previous error(s)
  mag 04, 2015 5:22:49 PM org.apache.catalina.core.StandardContext startInternal
  GRAVE: Error listenerStart
  mag 04, 2015 5:22:49 PM org.apache.catalina.core.StandardContext startInternal
  GRAVE: Context [/SpringMVCHibernate] startup failed due to previous errors
  mag 04, 2015 5:22:49 PM org.apache.coyote.AbstractProtocol start
  INFORMAZIONI: Starting ProtocolHandler ["http-nio-8080"]
  mag 04, 2015 5:22:49 PM org.apache.coyote.AbstractProtocol start
  INFORMAZIONI: Starting ProtocolHandler ["ajp-nio-8009"]
  mag 04, 2015 5:22:49 PM org.apache.catalina.startup.Catalina start
  INFORMAZIONI: Server startup in 315 ms
  </code></pre>
  
  <p>More precisely, the browser displays the error</p>
  
  <pre><code>HTTP Status 404 - /SpringMVCHibernate/
  
  
  type Status report
  
  message /SpringMVCHibernate/
  
  description The requested resource is not available.
  </code></pre>
  
  <p>Going to the following link you can see the structure of the project: <a href="https://www.dropbox.com/s/l3mqjuzbpz5yqxy/springmvchibernate.png?dl=0" rel="nofollow">https://www.dropbox.com/s/l3mqjuzbpz5yqxy/springmvchibernate.png?dl=0</a></p>
  
  <p>Below you can see the code in my application!</p>
  
  <p><strong>web.xml</strong></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;web-app version="2.5" 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_2_5.xsd"&gt;
  
      &lt;!-- The definition of the Root Spring Container shared by all Servlets and Filters --&gt;
      &lt;context-param&gt;
          &lt;param-name&gt;contextConfigLocation&lt;/param-name&gt;
          &lt;param-value&gt;/WEB-INF/spring/root-context.xml&lt;/param-value&gt;
      &lt;/context-param&gt;
  
      &lt;!-- Creates the Spring Container shared by all Servlets and Filters --&gt;
      &lt;listener&gt;
          &lt;listener-class&gt;org.springframework.web.context.ContextLoaderListener&lt;/listener-class&gt;
      &lt;/listener&gt;
  
      &lt;!-- Processes application requests --&gt;
      &lt;servlet&gt;
          &lt;servlet-name&gt;appServlet&lt;/servlet-name&gt;
          &lt;servlet-class&gt;org.springframework.web.servlet.DispatcherServlet&lt;/servlet-class&gt;
          &lt;init-param&gt;
              &lt;param-name&gt;contextConfigLocation&lt;/param-name&gt;
              &lt;param-value&gt;/WEB-INF/spring/appServlet/servlet-context.xml&lt;/param-value&gt;
          &lt;/init-param&gt;
          &lt;load-on-startup&gt;1&lt;/load-on-startup&gt;
      &lt;/servlet&gt;
  
      &lt;servlet-mapping&gt;
          &lt;servlet-name&gt;appServlet&lt;/servlet-name&gt;
          &lt;url-pattern&gt;/&lt;/url-pattern&gt;
      &lt;/servlet-mapping&gt;
  
  &lt;/web-app&gt;
  </code></pre>
  
  <p><strong>servlet-context.xml</strong></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;beans:beans xmlns="http://www.springframework.org/schema/mvc"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:beans="http://www.springframework.org/schema/beans"
      xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
      xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
          http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
          http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
          http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd"&gt;
  
      &lt;!-- DispatcherServlet Context: defines this servlet's request-processing 
          infrastructure --&gt;
  
      &lt;!-- Enables the Spring MVC @Controller programming model --&gt;
      &lt;annotation-driven /&gt;
  
      &lt;!-- Handles HTTP GET requests for /resources/** by efficiently serving 
          up static resources in the ${webappRoot}/resources directory --&gt;
      &lt;resources mapping="/resources/**" location="/resources/" /&gt;
  
      &lt;!-- Resolves views selected for rendering by @Controllers to .jsp resources 
          in the /WEB-INF/views directory --&gt;
      &lt;beans:bean
          class="org.springframework.web.servlet.view.InternalResourceViewResolver"&gt;
          &lt;beans:property name="prefix" value="/WEB-INF/views/" /&gt;
          &lt;beans:property name="suffix" value=".jsp" /&gt;
      &lt;/beans:bean&gt;
  
      &lt;beans:bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
          destroy-method="close"&gt;
          &lt;beans:property name="driverClassName" value="com.mysql.jdbc.Driver" /&gt;
          &lt;beans:property name="url"
              value="jdbc:mysql://localhost:3306/testdb" /&gt;
          &lt;beans:property name="username" value="root" /&gt;
          &lt;beans:property name="password" value="" /&gt;
      &lt;/beans:bean&gt;
  
      &lt;!-- Hibernate 4 SessionFactory Bean definition --&gt;
      &lt;beans:bean id="hibernate4AnnotatedSessionFactory"
          class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"&gt;
          &lt;beans:property name="dataSource" ref="dataSource" /&gt;
          &lt;beans:property name="annotatedClasses"&gt;
              &lt;beans:list&gt;
                  &lt;beans:value&gt;com.journaldev.spring.model.Person&lt;/beans:value&gt;
              &lt;/beans:list&gt;
          &lt;/beans:property&gt;
          &lt;beans:property name="hibernateProperties"&gt;
              &lt;beans:props&gt;
                  &lt;beans:prop key="hibernate.dialect"&gt;org.hibernate.dialect.MySQLDialect
                  &lt;/beans:prop&gt;
                  &lt;beans:prop key="hibernate.show_sql"&gt;true&lt;/beans:prop&gt;
              &lt;/beans:props&gt;
          &lt;/beans:property&gt;
      &lt;/beans:bean&gt;
  
      &lt;beans:bean id="personDAO" class="com.journaldev.spring.dao.PersonDAOImpl"&gt;
          &lt;beans:property name="sessionFactory" ref="hibernate4AnnotatedSessionFactory" /&gt;
      &lt;/beans:bean&gt;
      &lt;beans:bean id="personService" class="com.journaldev.spring.service.PersonServiceImpl"&gt;
          &lt;beans:property name="personDAO" ref="personDAO"&gt;&lt;/beans:property&gt;
      &lt;/beans:bean&gt;
  
      &lt;context:component-scan base-package="com.journaldev.spring" /&gt;
  
      &lt;tx:annotation-driven transaction-manager="transactionManager"/&gt;
  
      &lt;beans:bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager"&gt;
          &lt;beans:property name="sessionFactory" ref="hibernate4AnnotatedSessionFactory" /&gt;
      &lt;/beans:bean&gt;
  
  
  &lt;/beans:beans&gt;
  </code></pre>
  
  <p><strong>pom.xml</strong></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"&gt;
      &lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;
      &lt;groupId&gt;com.journaldev.spring&lt;/groupId&gt;
      &lt;artifactId&gt;SpringMVCHibernate&lt;/artifactId&gt;
      &lt;name&gt;SpringMVCHibernate&lt;/name&gt;
      &lt;packaging&gt;war&lt;/packaging&gt;
      &lt;version&gt;1.0.0-BUILD-SNAPSHOT&lt;/version&gt;
      &lt;properties&gt;
          &lt;java-version&gt;1.6&lt;/java-version&gt;
          &lt;org.springframework-version&gt;4.0.3.RELEASE&lt;/org.springframework-version&gt;
          &lt;org.aspectj-version&gt;1.7.4&lt;/org.aspectj-version&gt;
          &lt;org.slf4j-version&gt;1.7.5&lt;/org.slf4j-version&gt;
          &lt;hibernate.version&gt;4.3.5.Final&lt;/hibernate.version&gt;
      &lt;/properties&gt;
      &lt;dependencies&gt;
          &lt;!-- Spring --&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework&lt;/groupId&gt;
              &lt;artifactId&gt;spring-context&lt;/artifactId&gt;
              &lt;version&gt;${org.springframework-version}&lt;/version&gt;
              &lt;exclusions&gt;
                  &lt;!-- Exclude Commons Logging in favor of SLF4j --&gt;
                  &lt;exclusion&gt;
                      &lt;groupId&gt;commons-logging&lt;/groupId&gt;
                      &lt;artifactId&gt;commons-logging&lt;/artifactId&gt;
                   &lt;/exclusion&gt;
              &lt;/exclusions&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework&lt;/groupId&gt;
              &lt;artifactId&gt;spring-webmvc&lt;/artifactId&gt;
              &lt;version&gt;${org.springframework-version}&lt;/version&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework&lt;/groupId&gt;
              &lt;artifactId&gt;spring-tx&lt;/artifactId&gt;
              &lt;version&gt;${org.springframework-version}&lt;/version&gt;
          &lt;/dependency&gt;
  
          &lt;!-- Hibernate --&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.hibernate&lt;/groupId&gt;
              &lt;artifactId&gt;hibernate-core&lt;/artifactId&gt;
              &lt;version&gt;${hibernate.version}&lt;/version&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.hibernate&lt;/groupId&gt;
              &lt;artifactId&gt;hibernate-entitymanager&lt;/artifactId&gt;
              &lt;version&gt;${hibernate.version}&lt;/version&gt;
          &lt;/dependency&gt;
  
          &lt;!-- Apache Commons DBCP --&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;commons-dbcp&lt;/groupId&gt;
              &lt;artifactId&gt;commons-dbcp&lt;/artifactId&gt;
              &lt;version&gt;1.4&lt;/version&gt;
          &lt;/dependency&gt;
          &lt;!-- Spring ORM --&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework&lt;/groupId&gt;
              &lt;artifactId&gt;spring-orm&lt;/artifactId&gt;
              &lt;version&gt;${org.springframework-version}&lt;/version&gt;
          &lt;/dependency&gt;
  
          &lt;!-- AspectJ --&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.aspectj&lt;/groupId&gt;
              &lt;artifactId&gt;aspectjrt&lt;/artifactId&gt;
              &lt;version&gt;${org.aspectj-version}&lt;/version&gt;
          &lt;/dependency&gt;   
  
          &lt;!-- Logging --&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.slf4j&lt;/groupId&gt;
              &lt;artifactId&gt;slf4j-api&lt;/artifactId&gt;
              &lt;version&gt;${org.slf4j-version}&lt;/version&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.slf4j&lt;/groupId&gt;
              &lt;artifactId&gt;jcl-over-slf4j&lt;/artifactId&gt;
              &lt;version&gt;${org.slf4j-version}&lt;/version&gt;
              &lt;scope&gt;runtime&lt;/scope&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.slf4j&lt;/groupId&gt;
              &lt;artifactId&gt;slf4j-log4j12&lt;/artifactId&gt;
              &lt;version&gt;${org.slf4j-version}&lt;/version&gt;
              &lt;scope&gt;runtime&lt;/scope&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;log4j&lt;/groupId&gt;
              &lt;artifactId&gt;log4j&lt;/artifactId&gt;
              &lt;version&gt;1.2.15&lt;/version&gt;
              &lt;exclusions&gt;
                  &lt;exclusion&gt;
                      &lt;groupId&gt;javax.mail&lt;/groupId&gt;
                      &lt;artifactId&gt;mail&lt;/artifactId&gt;
                  &lt;/exclusion&gt;
                  &lt;exclusion&gt;
                      &lt;groupId&gt;javax.jms&lt;/groupId&gt;
                      &lt;artifactId&gt;jms&lt;/artifactId&gt;
                  &lt;/exclusion&gt;
                  &lt;exclusion&gt;
                      &lt;groupId&gt;com.sun.jdmk&lt;/groupId&gt;
                      &lt;artifactId&gt;jmxtools&lt;/artifactId&gt;
                  &lt;/exclusion&gt;
                  &lt;exclusion&gt;
                      &lt;groupId&gt;com.sun.jmx&lt;/groupId&gt;
                      &lt;artifactId&gt;jmxri&lt;/artifactId&gt;
                  &lt;/exclusion&gt;
              &lt;/exclusions&gt;
              &lt;scope&gt;runtime&lt;/scope&gt;
          &lt;/dependency&gt;
  
          &lt;!-- @Inject --&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;javax.inject&lt;/groupId&gt;
              &lt;artifactId&gt;javax.inject&lt;/artifactId&gt;
              &lt;version&gt;1&lt;/version&gt;
          &lt;/dependency&gt;
  
          &lt;!-- Servlet --&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;javax.servlet&lt;/groupId&gt;
              &lt;artifactId&gt;servlet-api&lt;/artifactId&gt;
              &lt;version&gt;2.5&lt;/version&gt;
              &lt;scope&gt;provided&lt;/scope&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;javax.servlet.jsp&lt;/groupId&gt;
              &lt;artifactId&gt;jsp-api&lt;/artifactId&gt;
              &lt;version&gt;2.1&lt;/version&gt;
              &lt;scope&gt;provided&lt;/scope&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;javax.servlet&lt;/groupId&gt;
              &lt;artifactId&gt;jstl&lt;/artifactId&gt;
              &lt;version&gt;1.2&lt;/version&gt;
          &lt;/dependency&gt;
  
          &lt;!-- Test --&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;junit&lt;/groupId&gt;
              &lt;artifactId&gt;junit&lt;/artifactId&gt;
              &lt;version&gt;4.7&lt;/version&gt;
              &lt;scope&gt;test&lt;/scope&gt;
          &lt;/dependency&gt;
  
          &lt;dependency&gt;
              &lt;groupId&gt;mysql&lt;/groupId&gt;
              &lt;artifactId&gt;mysql-connector-java&lt;/artifactId&gt;
              &lt;version&gt;5.1.35&lt;/version&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework&lt;/groupId&gt;
              &lt;artifactId&gt;spring-web&lt;/artifactId&gt;
              &lt;version&gt;4.1.6.RELEASE&lt;/version&gt;
          &lt;/dependency&gt;
      &lt;/dependencies&gt;
      &lt;build&gt;
          &lt;plugins&gt;
              &lt;plugin&gt;
                  &lt;artifactId&gt;maven-eclipse-plugin&lt;/artifactId&gt;
                  &lt;version&gt;2.9&lt;/version&gt;
                  &lt;configuration&gt;
                      &lt;additionalProjectnatures&gt;
                          &lt;projectnature&gt;org.springframework.ide.eclipse.core.springnature&lt;/projectnature&gt;
                      &lt;/additionalProjectnatures&gt;
                      &lt;additionalBuildcommands&gt;
                          &lt;buildcommand&gt;org.springframework.ide.eclipse.core.springbuilder&lt;/buildcommand&gt;
                      &lt;/additionalBuildcommands&gt;
                      &lt;downloadSources&gt;true&lt;/downloadSources&gt;
                      &lt;downloadJavadocs&gt;true&lt;/downloadJavadocs&gt;
                  &lt;/configuration&gt;
              &lt;/plugin&gt;
              &lt;plugin&gt;
                  &lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt;
                  &lt;artifactId&gt;maven-compiler-plugin&lt;/artifactId&gt;
                  &lt;version&gt;2.5.1&lt;/version&gt;
                  &lt;configuration&gt;
                      &lt;source&gt;1.6&lt;/source&gt;
                      &lt;target&gt;1.6&lt;/target&gt;
                      &lt;compilerArgument&gt;-Xlint:all&lt;/compilerArgument&gt;
                      &lt;showWarnings&gt;true&lt;/showWarnings&gt;
                      &lt;showDeprecation&gt;true&lt;/showDeprecation&gt;
                  &lt;/configuration&gt;
              &lt;/plugin&gt;
              &lt;plugin&gt;
                  &lt;groupId&gt;org.codehaus.mojo&lt;/groupId&gt;
                  &lt;artifactId&gt;exec-maven-plugin&lt;/artifactId&gt;
                  &lt;version&gt;1.2.1&lt;/version&gt;
                  &lt;configuration&gt;
                      &lt;mainClass&gt;org.test.int1.Main&lt;/mainClass&gt;
                  &lt;/configuration&gt;
              &lt;/plugin&gt;
          &lt;/plugins&gt;
          &lt;finalName&gt;${project.artifactId}&lt;/finalName&gt;
      &lt;/build&gt;
  &lt;/project&gt;
  </code></pre>
  
  <p><strong>Person.java</strong></p>
  
  <pre><code>package com.journaldev.spring.model;
  
  import javax.persistence.Column;
  import javax.persistence.Entity;
  import javax.persistence.GeneratedValue;
  import javax.persistence.GenerationType;
  import javax.persistence.Id;
  import javax.persistence.Table;
  
  /**
   * Entity bean with JPA annotations
   * Hibernate provides JPA implementation
   * @author pankaj
   *
   */
  @Entity
  @Table(name="PERSON")
  public class Person {
  
      @Id
      @Column(name="id")
      @GeneratedValue(strategy=GenerationType.IDENTITY)
      private int id;
  
      private String name;
  
      private String country;
  
      public int getId() {
          return id;
      }
  
      public void setId(int id) {
          this.id = id;
      }
  
      public String getName() {
          return name;
      }
  
      public void setName(String name) {
          this.name = name;
      }
  
      public String getCountry() {
          return country;
      }
  
      public void setCountry(String country) {
          this.country = country;
      }
  
      @Override
      public String toString(){
          return "id="+id+", name="+name+", country="+country;
      }
  }
  </code></pre>
  
  <p><strong>PersonService.java</strong></p>
  
  <pre><code>package com.journaldev.spring.service;
  
  import java.util.List;
  
  import com.journaldev.spring.model.Person;
  
  public interface PersonService {
  
      public void addPerson(Person p);
      public void updatePerson(Person p);
      public List&lt;Person&gt; listPersons();
      public Person getPersonById(int id);
      public void removePerson(int id);
  
  }
  </code></pre>
  
  <p><strong>PersonServiceImpl.java</strong></p>
  
  <pre><code>package com.journaldev.spring.service;
  
  import java.util.List;
  
  import org.springframework.stereotype.Service;
  import org.springframework.transaction.annotation.Transactional;
  
  import com.journaldev.spring.dao.PersonDAO;
  import com.journaldev.spring.model.Person;
  
  @Service
  public class PersonServiceImpl implements PersonService {
  
      private PersonDAO personDAO;
  
      public void setPersonDAO(PersonDAO personDAO) {
          this.personDAO = personDAO;
      }
  
      @Override
      @Transactional
      public void addPerson(Person p) {
          this.personDAO.addPerson(p);
      }
  
      @Override
      @Transactional
      public void updatePerson(Person p) {
          this.personDAO.updatePerson(p);
      }
  
      @Override
      @Transactional
      public List&lt;Person&gt; listPersons() {
          return this.personDAO.listPersons();
      }
  
      @Override
      @Transactional
      public Person getPersonById(int id) {
          return this.personDAO.getPersonById(id);
      }
  
      @Override
      @Transactional
      public void removePerson(int id) {
          this.personDAO.removePerson(id);
      }
  
  }
  </code></pre>
  
  <p><strong>PersonDAO.java</strong></p>
  
  <pre><code>package com.journaldev.spring.dao;
  
  import java.util.List;
  
  import com.journaldev.spring.model.Person;
  
  public interface PersonDAO {
  
      public void addPerson(Person p);
      public void updatePerson(Person p);
      public List&lt;Person&gt; listPersons();
      public Person getPersonById(int id);
      public void removePerson(int id);
  }
  </code></pre>
  
  <p><strong>PersonDAOImpl.java</strong></p>
  
  <pre><code>package com.journaldev.spring.dao;
  
  import java.util.List;
  
  import org.hibernate.Session;
  import org.hibernate.SessionFactory;
  import org.slf4j.Logger;
  import org.slf4j.LoggerFactory;
  import org.springframework.stereotype.Repository;
  
  import com.journaldev.spring.model.Person;
  
  @Repository
  public class PersonDAOImpl implements PersonDAO {
  
      private static final Logger logger = LoggerFactory.getLogger(PersonDAOImpl.class);
  
      private SessionFactory sessionFactory;
  
      public void setSessionFactory(SessionFactory sf){
          this.sessionFactory = sf;
      }
  
      @Override
      public void addPerson(Person p) {
          Session session = this.sessionFactory.getCurrentSession();
          session.persist(p);
          logger.info("Person saved successfully, Person Details="+p);
      }
  
      @Override
      public void updatePerson(Person p) {
          Session session = this.sessionFactory.getCurrentSession();
          session.update(p);
          logger.info("Person updated successfully, Person Details="+p);
      }
  
      @SuppressWarnings("unchecked")
      @Override
      public List&lt;Person&gt; listPersons() {
          Session session = this.sessionFactory.getCurrentSession();
          List&lt;Person&gt; personsList = session.createQuery("from Person").list();
          for(Person p : personsList){
              logger.info("Person List::"+p);
          }
          return personsList;
      }
  
      @Override
      public Person getPersonById(int id) {
          Session session = this.sessionFactory.getCurrentSession();      
          Person p = (Person) session.load(Person.class, new Integer(id));
          logger.info("Person loaded successfully, Person details="+p);
          return p;
      }
  
      @Override
      public void removePerson(int id) {
          Session session = this.sessionFactory.getCurrentSession();
          Person p = (Person) session.load(Person.class, new Integer(id));
          if(null != p){
              session.delete(p);
          }
          logger.info("Person deleted successfully, person details="+p);
      }
  
  }
  </code></pre>
  
  <p><strong>PersonController.java</strong></p>
  
  <pre><code>package com.journaldev.spring;
  
  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.beans.factory.annotation.Qualifier;
  import org.springframework.stereotype.Controller;
  import org.springframework.ui.Model;
  import org.springframework.web.bind.annotation.ModelAttribute;
  import org.springframework.web.bind.annotation.PathVariable;
  import org.springframework.web.bind.annotation.RequestMapping;
  import org.springframework.web.bind.annotation.RequestMethod;
  
  import com.journaldev.spring.model.Person;
  import com.journaldev.spring.service.PersonService;
  
  @Controller
  public class PersonController {
  
      private PersonService personService;
  
      @Autowired(required=true)
      @Qualifier(value="personService")
      public void setPersonService(PersonService ps){
          this.personService = ps;
      }
  
      @RequestMapping(value = "/persons", method = RequestMethod.GET)
      public String listPersons(Model model) {
          model.addAttribute("person", new Person());
          model.addAttribute("listPersons", this.personService.listPersons());
          return "person";
      }
  
      //For add and update person both
      @RequestMapping(value= "/person/add", method = RequestMethod.POST)
      public String addPerson(@ModelAttribute("person") Person p){
  
          if(p.getId() == 0){
              //new person, add it
              this.personService.addPerson(p);
          }else{
              //existing person, call update
              this.personService.updatePerson(p);
          }
  
          return "redirect:/persons";
  
      }
  
      @RequestMapping("/remove/{id}")
      public String removePerson(@PathVariable("id") int id){
  
          this.personService.removePerson(id);
          return "redirect:/persons";
      }
  
      @RequestMapping("/edit/{id}")
      public String editPerson(@PathVariable("id") int id, Model model){
          model.addAttribute("person", this.personService.getPersonById(id));
          model.addAttribute("listPersons", this.personService.listPersons());
          return "person";
      }
  
  }
  </code></pre>
  
  <p><em>I trust in your help, thank you!</em></p>