Announcement Announcement Module
No announcement yet.
Getting the value from the HTTP request rather than from database Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • Getting the value from the HTTP request rather than from database


    I am using Spring MVC for my web layer and Hibernate for the persistence layer. I am facing a strange problem when trying to access the value of an object from the database.

    I have a model class named 'User' with a property named 'userName' of String class. In the controller, I am doing a check to see if the 'userName' has changed in the onSubmit() method.

    This is the way I am trying to do:

    public ModelAndView onSubmit(HttpServletRequest request,
                                     HttpServletResponse response, Object command,
                                     BindException errors)
        throws Exception {
            User user = (User) command;
            if(null != command.getId()) {
                  String newUserName = command.getUserName();
                  String oldUserName = userManager.getUser(command.getId())
                       // Do some custom processing

    The userManager delegates it to the DAO layer and the implementation is given below:

    public User getUser(Long userId) {
            User user = (User) getHibernateTemplate().get(User.class, userId);
            if (user == null) {
                log.warn("uh oh, user '" + userId + "' not found...");
                throw new ObjectRetrievalFailureException(User.class, userId);
            return user;

    My problem is that I am not able to get the value for userName through the Manager layer. For some strange reason ,it is giving me the 'userName' for the command object even though I haven't saved it to the database.

    Any ideas or suggestions would be very helpful and if anybody needs any clarification regarding the description of this problem, please let me know.

  • #2
    Where do you get the command object from? Are you using Hibernate with OpenSesssionInView? How the transactions demarcated?
    Everything seems to point to either the direct write-through of the user name to the database or the user manager being aware of the change in user name.



    • #3
      First of all, apologies if I am not very clear as I am very new to Hibernate. Initially, I get the command object from the in the formBackingObject() through the usual way by calling the manager layer and the manager layer delegation the call to the DAO layer.

      I am using the Open Session in View pattern and given below is the transaction snippet that I have in the appContext-service.xml file:

      <bean id="txProxyTemplate" abstract="true"
              <property name="transactionManager" ref="transactionManager"/>
              <property name="transactionAttributes">
                      <prop key="save*">PROPAGATION_REQUIRED</prop>
                      <prop key="remove*">PROPAGATION_REQUIRED</prop>
                      <prop key="import*">PROPAGATION_REQUIRED</prop>
                      <prop key="*">PROPAGATION_REQUIRED,readOnly</prop>
      Any suggestions or guidance would be very helpful. Thanks!!


      • #4
        Hibernate is giving you one object per session representing the database entry. This means with OSIV and Spring's data binding you already changed Hibernate's object for the database entry. That's why the object retrieved from the manager has already the new value. If I remember correctly you can even compare the two objects command and userManager.getUser(command.getId()) for equality by using ==.



        • #5
          Thanks for your reply. So how do you do the comparison to see if a value has changed in the onSubmit method? In other words, how can you get a copy of the old instance?



          • #6
            Make your commandobject a session object, that way it will be retrieved from the database only once.


            • #7
              Originally posted by joejakes View Post
              So how do you do the comparison to see if a value has changed in the onSubmit method? In other words, how can you get a copy of the old instance?
              Your question reminded me of something like preserving old values in BeanWrapperImpl. So I looked into the code and found PropertyChangeEvent - which made me think: Wait, I already have investigated this code more deeply ... indeed it was me who had similar requirements before No solution for this though ...