title: Spring MVC Hibernate Application loading database table
body: <p>I created a simple web application with SpringMVC and Hibernate. Everything works fine, my employees are added to database (using MySQL) but page don't show the list of them:</p>
  
  <p><img src="https://i.stack.imgur.com/fl4fL.jpg" alt="that&#39;s how it look"></p>
  
  <p>Also logger don't show any output message when I'm adding the object when it should show something like: "Employee add successfully, Employee details: ..."</p>
  
  <p>Here's the page: </p>
  
  <pre><code>&lt;%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %&gt;
  &lt;%@ taglib uri="http://www.springframework.org/tags" prefix="spring" %&gt;
  &lt;%@ taglib uri="http://www.springframework.org/tags/form" prefix="form" %&gt;
  &lt;%@ page session="false" %&gt;
  &lt;html&gt;
  &lt;head&gt;
      &lt;title&gt;Employee Page&lt;/title&gt;
      &lt;style type="text/css"&gt;
          .tg  {border-collapse:collapse;border-spacing:0;border-color:#ccc;}
          .tg td{font-family:Arial, sans-serif;font-size:14px;padding:10px 5px;border-style:solid;border-width:1px;overflow:hidden;word-break:normal;border-color:#ccc;color:#333;background-color:#fff;}
          .tg th{font-family:Arial, sans-serif;font-size:14px;font-weight:normal;padding:10px 5px;border-style:solid;border-width:1px;overflow:hidden;word-break:normal;border-color:#ccc;color:#333;background-color:#f0f0f0;}
          .tg .tg-4eph{background-color:#f9f9f9}
      &lt;/style&gt;
  &lt;/head&gt;
  &lt;body&gt;
  &lt;h1&gt;
      Add a Employee
  &lt;/h1&gt;
  
  &lt;c:url var="addAction" value="/employee/add" &gt;&lt;/c:url&gt;
  
  &lt;form:form action="${addAction}" commandName="employee"&gt;
      &lt;table&gt;
          &lt;c:if test="${!empty employee.name}"&gt;
              &lt;tr&gt;
                  &lt;td&gt;
                      &lt;form:label path="id"&gt;
                          &lt;spring:message text="ID"/&gt;
                      &lt;/form:label&gt;
                  &lt;/td&gt;
                  &lt;td&gt;
                      &lt;form:input path="id" readonly="true" size="8"  disabled="true" /&gt;
                      &lt;form:hidden path="id" /&gt;
                  &lt;/td&gt;
              &lt;/tr&gt;
          &lt;/c:if&gt;
          &lt;tr&gt;
              &lt;td&gt;
                  &lt;form:label path="name"&gt;
                      &lt;spring:message text="Name"/&gt;
                  &lt;/form:label&gt;
              &lt;/td&gt;
              &lt;td&gt;
                  &lt;form:input path="name" /&gt;
              &lt;/td&gt;
          &lt;/tr&gt;
          &lt;tr&gt;
              &lt;td colspan="2"&gt;
                  &lt;c:if test="${!empty employee.name}"&gt;
                      &lt;input type="submit"
                             value="&lt;spring:message text="Edit Employee"/&gt;" /&gt;
                  &lt;/c:if&gt;
                  &lt;c:if test="${empty employee.name}"&gt;
                      &lt;input type="submit"
                             value="&lt;spring:message text="Add Employee"/&gt;" /&gt;
                  &lt;/c:if&gt;
              &lt;/td&gt;
          &lt;/tr&gt;
      &lt;/table&gt;
  &lt;/form:form&gt;
  &lt;br&gt;
  &lt;h3&gt;List of Employees&lt;/h3&gt;
  &lt;c:if test="${!empty listEmployees}"&gt;
      &lt;table class="tg"&gt;
          &lt;tr&gt;
              &lt;th width="80"&gt;Employee ID&lt;/th&gt;
              &lt;th width="120"&gt;Employee Name&lt;/th&gt;
              &lt;th width="60"&gt;Edit&lt;/th&gt;
              &lt;th width="60"&gt;Delete&lt;/th&gt;
          &lt;/tr&gt;
          &lt;c:forEach items="${listEmployee}" var="employee"&gt;
              &lt;tr&gt;
                  &lt;td&gt;${employee.id}&lt;/td&gt;
                  &lt;td&gt;${employee.name}&lt;/td&gt;
                  &lt;td&gt;&lt;a href="&lt;c:url value='/edit/${employee.id}' /&gt;" &gt;Edit&lt;/a&gt;&lt;/td&gt;
                  &lt;td&gt;&lt;a href="&lt;c:url value='/remove/${employee.id}' /&gt;" &gt;Delete&lt;/a&gt;&lt;/td&gt;
              &lt;/tr&gt;
          &lt;/c:forEach&gt;
      &lt;/table&gt;
  &lt;/c:if&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  
  @Qualifier(value = "employeeDAO")
  public class EmployeeDAOImpl implements EmployeeDAO {
  
      private static final Logger logger = LoggerFactory.getLogger(EmployeeDAOImpl.class);
  
      private SessionFactory sessionFactory;
  
      public void setSessionFactory(SessionFactory sf){
          this.sessionFactory = sf;
      }
      @Override
      public void addEmployee(Employee e) {
          Session session = this.sessionFactory.getCurrentSession();
          session.persist(e);
          logger.info("Employee saved successfully, Employee details: " + e);
      }
  
      @Override
      public void updateEmployee(Employee e) {
          Session session = this.sessionFactory.getCurrentSession();
          session.update(e);
          logger.info("Employee updated successfully, Employee details: " + e);
      }
  
      @SuppressWarnings("uncheked")
      @Override
      public List&lt;Employee&gt; listEmployee() {
          Session session = this.sessionFactory.getCurrentSession();
          List&lt;Employee&gt; employeeList = session.createQuery("FROM Employee").list();
          for (Employee e: employeeList){
              logger.info("Employee List::" + e);
          }
          return employeeList;
  
      }
  
      @Override
      public Employee getEmployeeById(int id) {
          Session session = this.sessionFactory.getCurrentSession();
          Employee e = (Employee) session.load(Employee.class, new Integer(id));
          logger.info("Employee loaded successfully, Employee details: " + e);
          return null;
      }
  
      @Override
      public void removeEmployee(int id) {
          Session session = this.sessionFactory.getCurrentSession();
          Employee e = (Employee) session.load(Employee.class, new Integer(id));
          if(e != null){
              session.delete(e);
          }
          logger.info("Employee delete successfully, Employee details: " + e);
      }
  }
  </code></pre>
  
  <p>My Service class:</p>
  
  <pre><code>@Service
  @Qualifier(value = "employeeService")
  public class EmployeeServiceImpl implements EmployeeService{
  
  
  
      public EmployeeServiceImpl() {
      }
  
      private EmployeeDAO employeeDAO;
  
      public EmployeeServiceImpl(EmployeeDAO employeeDAO) {
          this.employeeDAO = employeeDAO;
      }
  
      @Override
      @Transactional
      public void addEmployee(Employee e) {
          this.employeeDAO.addEmployee(e);
      }
  
      @Override
      @Transactional
      public void updateEmployee(Employee e) {
          this.employeeDAO.updateEmployee(e);
      }
  
      @Override
      @Transactional
      public List&lt;Employee&gt; listEmployee() {
          return this.employeeDAO.listEmployee();
      }
  
      @Override
      @Transactional
      public Employee getEmployeeById(int id) {
          return this.employeeDAO.getEmployeeById(id);
      }
  
      @Override
      @Transactional
      public void removeEmployee(int id) {
          this.employeeDAO.removeEmployee(id);
      }
  
      public void setEmployeeDAO(EmployeeDAOImpl employeeDAO) {
          this.employeeDAO = employeeDAO;
      }
  }
  </code></pre>
  
  <p>Here I create the mapping:</p>
  
  <pre><code>@Entity
  @Table(name = "Employee")
  public class Employee {
      @Column(name = "Name")
      private String name;
  
      @Id
      @Column(name = "Employee ID")
      @GeneratedValue(strategy = GenerationType.IDENTITY)
      private int employeeID;
  
      public Employee(String name) {
          this.name = name;
      }
  
      public Employee() {
      }
  
      public String getName() {
          return name;
      }
  
      public void setName(String name) {
          this.name = name;
      }
  
      public int getEmployeeID() {
          return employeeID;
      }
  
      public void setEmployeeID(int employeeID) {
          this.employeeID = employeeID;
      }
  
      @Override
      public String toString() {
          return "Employee{" +
                  "name='" + name + '\'' +
                  ", employeeID=" + employeeID +
                  '}';
      }
  }
  </code></pre>
  
  <p>Controller class:</p>
  
  <pre><code>@Controller
  public class EmployeeController {
  
      private EmployeeService employeeService;
  
      @Autowired(required = true)
      @Qualifier("employeeService")
      public void setEmployeeService(EmployeeService employeeService) {
          this.employeeService = employeeService;
      }
  
      @RequestMapping(value = "/employee", method = RequestMethod.GET)
      public String employeeList(Model model) {
          model.addAttribute("employee", new Employee());
          model.addAttribute("listEmployee", this.employeeService.listEmployee());
          return "employee";
      }
  
      //For add and update person both
      @RequestMapping(value = "/employee/add", method = RequestMethod.GET)
      public String addEmployee(@ModelAttribute("person") Employee e){
  
          if (e.getEmployeeID() == 0){
              //new employee, add it
              this.employeeService.addEmployee(e);
          }else {
              //existing employee, call update
              this.employeeService.updateEmployee(e);
          }
          return "redirect:/employee";
      }
  
      @RequestMapping(value = "employees/remove/{id}")
      public String removeEmployee(@PathVariable("id") int id){
  
          this.employeeService.removeEmployee(id);
          return "redirect:/employees";
      }
  
      @RequestMapping(value = "/employees/edit{id}")
      public String editEmployee(@PathVariable("id") int id, Model model){
  
          model.addAttribute("employee", this.employeeService.getEmployeeById(id));
          model.addAttribute("listEmployees", this.employeeService.listEmployee());
          return "employee";
      }
  }
  </code></pre>