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

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

    Hi,

    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:

    Code:
    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())
                                  .getUserName();
    
                  if(!newUserName.equalsIgnoreCase(oldUserName)){
                       // Do some custom processing

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

    Code:
    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.

    Joerg

    Comment


    • #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:

      Code:
      <bean id="txProxyTemplate" abstract="true"
              class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
              <property name="transactionManager" ref="transactionManager"/>
              <property name="transactionAttributes">
                  <props>
                      <prop key="save*">PROPAGATION_REQUIRED</prop>
                      <prop key="remove*">PROPAGATION_REQUIRED</prop>
                      <prop key="import*">PROPAGATION_REQUIRED</prop>
                      <prop key="*">PROPAGATION_REQUIRED,readOnly</prop>
                  </props>
              </property>
       </bean>
      Any suggestions or guidance would be very helpful. Thanks!!

      Comment


      • #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 ==.

        Joerg

        Comment


        • #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?

          Thanks!

          Comment


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

            Comment


            • #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 ...

              Joerg

              Comment

              Working...
              X