4 Replies Latest reply on Jul 7, 2008 8:09 AM by Terry

    Checking if user modified data before calling update

    Terry Apprentice

      When using Seam EntityHome objects with jsf edit page. is it possible to check in backing bean whether user changed any data on the page committed?


      I like to make that check before calling entityHome Update methods in the backing bean.


        • 1. Re: Checking if user modified data before calling update
          Samuel Mendenhall Apprentice

          There isn't a straightforward way to do this, not sure why you would want to though.  Since you are modifying the instance in EntityHome, ie. #{entityHome.instance.name} for ex., then there is nothing to compare the instance against.  You would actually have to reread the entity into another variable and compare fields with the instance or override equals/hashcode, which doesn't make sense.  Hibernate will handle updating the entity in the db.


          If EntityHome is too restrictive for you, you could override methods to suit your needs, or you could create a new class like CustomEntityHome and implement the exact behavior you want.


          -Samuel

          • 2. Re: Checking if user modified data before calling update
            Terry Apprentice

            yes Samuel hibernate does not update database if entity values have not been changed. however Seam entityHome's update, persist methods still return updated, persisted.


            I have user activity logging function which relies on status returned by updated, persisted, deleted methods. I wanted to log success activity log only if record actually got updated in database.


            even if I overwrite entityHome's persist, update methods etc, not sure how to check whether hibernate actually updated the database or not. I guess I will have to look deeper in to it....


            any clues on this are appreciated.


            Cheers,
            Terry

            • 3. Re: Checking if user modified data before calling update
              Dan Allen Master

              This is not a feature in JPA, but it is available in Hibernate. The Hibernate Session has a method named isDirty() which will tell you if there are any outstanding DML statements (database writes). You don't know what they are, you just know they exist.


              There are two ways to get at this data.


              boolean dirty = ((Session) entityManager.getDelegate()).isDirty();
              



              or


              <factory name="hibernateSession" value="#{entityManager.delegate}"
                auto-create="true" scope="stateless"/>
              
              @In Session hibernateSession;
              
              boolean dirty = hibernateSession.isDirty();
              



              Enjoy!

              • 4. Re: Checking if user modified data before calling update
                Terry Apprentice

                Thanks Dan, exactly what I need :).