3 Replies Latest reply on May 23, 2008 11:45 PM by Dan Allen

    Flushing only subset of list entities

    tomas kral Newbie

      Hello,


      I encouter following problem:


      There is for example 10 entities readed from database.
      There is conversation started for this purpose.
      Entity manger is set to FlushModeTYPE.MANUAL.


      User edits all of these entities, but only 5 are right validated (in business layer).


      So I want only 5 entities to update and 5 entities not to update in DB.


      But all of these entities are stored in DB.


      public void save(List<Person> persons) {
      
        for(Person person : persons) {
          if (validated(person)) {
            this.entityManager.merge(person);
          }
        }
      
        this.entityManager.flush();  //all entities changes are flushed here??
      }
      



      So the question is how to update only 5 validated entities?


      (If they are managed with seam maneged persistence context.)

        • 1. Re: Flushing only subset of list entities
          Stephen Friedrich Novice

          IMHO that's a somewhat strange scenario, but anyway:


          I think that there's no easy way to do this.
          If it is ok for the changes in the invalid objects to be lost, then you could call entityManager.refresh(object) for these objects.


          If you are using hibernate, you could fall back to the hibernate api for removing single instances from the persistence context (err, hibernate session):



          ((Session)entityManager.getDelegate()).evict(object)

          (or something like that - haven't tested it).
          Of course after that you have detached objects that are never going to automatically get flushed to the DB again :-(
          You can then merge them when their changes should be persisted later.


          By far the best solution would be to argue you user/client into changing the requirements - but I know all too well, that often this isn't possible.



          • 2. Re: Flushing only subset of list entities
            tomas kral Newbie

            Thanks for reply.
            I think this is often common scenario.
            I thought it wouldn't be easy :)


            I solved it with ordinary persistence context (leaving use of seam managed persistence context).
            Entities are not merged after each method call, so I decide what entity to persist and when.


            I can't use because changed values are lost.


            entityManager.refresh(object)


            • 3. Re: Flushing only subset of list entities
              Dan Allen Master

              Your best bet here is to make changes to a copy of the entities and then migrate those changes onto the managed entities when they are ready, at which time a flush occurs. Please note that you never have to use merge() if the persistence context stays open. That method is only needed to bring detached entities back into a persistence context (and the database).