1 Reply Latest reply on Aug 23, 2016 8:44 AM by Andy Frédéric

    Date in jsp is not same as nls_date_format spfile value

    Andy Frédéric

      The database is Oracle 11g2 Express.

       

      I changed the nls_date_format to DD/MM/RRRR :

       

      alter system set nls_date_format = "DD/MM/RRRR" scope = spfile;
      

       

        Then I restarted the database.

       

      Now in my spring-mvc project I created a Bean :

       

      @Entity
      @Table(name = "HR.EMPLOYEES")
      public class User {
      
          @Id
          @SequenceGenerator(name="EMPLOYEES_SEQ", sequenceName="EMPLOYEES_SEQ", allocationSize=1)
          @GeneratedValue(strategy=GenerationType.SEQUENCE, generator="EMPLOYEES_SEQ")
          @Column(name = "EMPLOYEE_ID")
          private int id;
          
          @Formula(value="FIRST_NAME || ' ' || LAST_NAME")
          private String username;
          
          @Column(name = "FIRST_NAME")
          private String firstname;
          
          @Column(name = "LAST_NAME")
          private String lastname;
          
          private String email;
          
          @Column(name="HIRE_DATE")
          @Temporal(TemporalType.DATE)
          private Date hireDate; // java.util.Date
          
          private String job_id;
          
          @Column(name = "SALARY")
          private double salary;
          
          public int getId() {
              return id;
          }
          public void setId(int id) {
              this.id = id;
          }
          public String getUsername() {
              return username;
          }
          public void setUsername(String username) {
              this.username = username;
          }
          public String getFirstname() {
              return firstname;
          }
          public void setFirstname(String firstname) {
              this.firstname = firstname;
          }
          public String getLastname() {
              return lastname;
          }
          public void setLastname(String lastname) {
              this.lastname = lastname;
          }
          public String getEmail() {
              return email;
          }
          public void setEmail(String email) {
              this.email = email;
          }
          public Date getHireDate() {
              return hireDate;
          }
          public void setHireDate(Date hireDate) {
              this.hireDate = hireDate;
          }
          public String getJob_id() {
              return job_id;
          }
          public void setJob_id(String job_id) {
              this.job_id = job_id;
          }
          public double getSalary() {
              return salary;
          }
          public void setSalary(double salary) {
              this.salary = salary;
          }
          
      }
      
      
      

       

      Here is DAO Implementation :

       

      public class UserDAOImpl implements UserDAO {
          
          @Autowired
          private SessionFactory sessionFactory;
          
          public UserDAOImpl() {
               
          }
          
          public UserDAOImpl(SessionFactory sessionFactory) {
              this.sessionFactory = sessionFactory;
          }
      
          @Override
          @Transactional
          public List<User> list() {        
              @SuppressWarnings("unchecked")
              List<User> listUser = (List<User>) sessionFactory.getCurrentSession()
              .createCriteria(User.class)
              .setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY).list();
              return listUser;
          }
      
          @Override
          @Transactional
          public User get(int id) {
              
              String hql = "from User where id=" + id;
              Query query = sessionFactory.getCurrentSession().createQuery(hql);
               
              @SuppressWarnings("unchecked")
              List<User> listUser = (List<User>) query.list();
               
              if (listUser != null && !listUser.isEmpty()) {
                  return listUser.get(0);
              }
               
              return null;
              
          }
      
          @Override
          @Transactional
          public void saveOrUpdate(User user) {
              sessionFactory.getCurrentSession().saveOrUpdate(user);
          }
      
          @Override
          @Transactional
          public void delete(int id) {
              User userToDelete = new User();
              userToDelete.setId(id);
              sessionFactory.getCurrentSession().delete(userToDelete);
          }
      
      }
      

       

      Here is the controller :

       

      @Controller
      public class HomeController {
          
          @Autowired
          private UserDAO userDao;
      
          @RequestMapping("/")
          public ModelAndView handleRequest() throws Exception { // to list employees
              List<User> listUsers = userDao.list();
              ModelAndView model = new ModelAndView("UserList");
              model.addObject("userList", listUsers);
              return model;
          }
           
          @RequestMapping(value = "/new", method = RequestMethod.GET)
          public ModelAndView newUser() { // to add a new employee
              ModelAndView model = new ModelAndView("UserForm");
              model.addObject("user_details", new User());
              model.addObject("titre", "Ajout");
              return model;      
          }
           
          @RequestMapping(value = "/edit", method = RequestMethod.GET)
          public ModelAndView editUser(HttpServletRequest request) { // to edit an existing employee
              int userId = Integer.parseInt(request.getParameter("id"));
              User user = userDao.get(userId);
              ModelAndView model = new ModelAndView("UserForm");
              model.addObject("user_details", user);
              model.addObject("titre", "Modif");
              return model;      
          }
           
          @RequestMapping(value = "/delete", method = RequestMethod.GET)
          public ModelAndView deleteUser(HttpServletRequest request) { // to delete an existing employee
              int userId = Integer.parseInt(request.getParameter("id"));
              userDao.delete(userId);
              return new ModelAndView("redirect:/");     
          }
           
          @RequestMapping(value = "/save", method = RequestMethod.POST)
          public ModelAndView saveUser(@ModelAttribute User user) { // to execute the dml of insert of update about an employee's data
              userDao.saveOrUpdate(user);
              return new ModelAndView("redirect:/");
          }
          
      }
      

       

      Here is the JSP for editing an employee :

       

       

      At runtime the value displayed in the text field for hire_date has a format RRRR-MM-DD , and the only accepted format to be able to be inserted/updated into the database is MM/DD/RRRR ! Although spfile nls_date_format is already set to DD/MM/RRRR and database is already restarted ! So why is the spfile parameter not effect !