3 Replies Latest reply on Feb 15, 2010 6:26 PM by thokuest

    Partially updating an entity

    rnicholson10

      Scenario:


      I have a conversation which is actively modifying several entities. The majority of the time only some of the fields are modified. There is an external application that updates some of the less frequently modified fields. The external application is not a J2EE application, I believe it's written in C.


      The issue I see is that when the external app updates a record the entity in our app essentially contains stale data. Even though our app does not modify the data that was updated by the external app once the entity is flushed it writes the entire entity to the db.


      My questions is: what would be the best way get the entity to only update the fields that had changed instead of updating the entire entity?


      I would assume that this is a common use case and that there must be recommended solution for doing this in Seam.


      Thanks for your help,


      Ross

        • 1. Re: Partially updating an entity
          ssamayoagt

          AFAIK JPA has no way to known which fields were modified, it knows only that the entity instance must be updated.


          The only way I see now is refreshing the instance, compare values, set those which changed then apply the changes in the recently retrieved instance.


          Also check your application for changes in the entity because one thing is for sure: flush only updates database if some of the setXXXX methods were called even if with the same value.


          Regards.

          • 2. Re: Partially updating an entity
            damianharvey.damianharvey.gmail.com

            Read up on the @Version annotation for Hibernate. You may be able to artifically increase the version from the C application. Big question marks here so worth checking out. This would only allow your app to generate an OptimisticLockException (or something like that).


            AFAIK you can't partially update an entity out-of-the-box but you could store the entity in another object, then refresh the entity from the database (entityManager.refresh()) then compare the values and only update what you want. Messy & fussy but would work. I do similar to generate version history for entity changes (eg. User X did this)


            Cheers,


            Damian.

            • 3. Re: Partially updating an entity
              thokuest

              what would be the best way get the entity to only update the fields that had changed instead of updating the entire entity?


              Using Hibernate, you could try using dynamic-update (Mapping Reference).