2 Replies Latest reply on Aug 27, 2009 9:06 AM by crnobog

    EntityQuery: Qury data before flush

    crnobog

      Here's my problem.


      I have an Entity query that contains data that is directly related to main entity (a classic one to many relation). Let's call these two entities Parent and Child


      I have a long running conversation, that flushes the data when user clicks save button for the Parent. Things work as expected as all the changes made to Children are saved to database with the parent and if the user somehow abandons the conversation all the changes are canceled. So far so good.
      Thing start to get a bit weird when I start dealing with adding or removing data from the Child table. Here's how I do it:


      Child c=new Child();
      c.setParent(activeParent);
      persist(c);
      childList.getResultList().add(c);
      



      childList is a instance of EntityQuery.


      Things work until the child is somehow refreshed as it disappears from the list, until the transaction is flushed.


      Is there a way of convincing the hibernate to include the unflushed child in the result of the Query?
      Is my approach to this any good at all?
      What are the alternatives to achieve this behavior?


      Thanks for your answers.





        • 1. Re: EntityQuery: Qury data before flush

          Is there a way of convincing the hibernate to include the unflushed child in the result of the Query?

          Is there a reason you don't want to flush newly persisted child? I would modify the code to be


          Child c=new Child();
          c.setParent(activeParent);
          persist(c);
          childList.refresh();
          



          The above code assumes that the persist() method is an extension of an EntityHome. You could do away with the childList.refresh() call if you @Override the ChildQuery.refresh method and add an @Observer to receive the org.jboss.seam.afterTransactionSuccess.Child event callback.

          • 2. Re: EntityQuery: Qury data before flush
            crnobog

            Thank you for your answer Matt



            Is there a reason you don't want to flush newly persisted child?


            Yes there is. I would like to treat the whole thing as one (complex) entity. So if a user makes some changes to the Child list and cancels the conversation before saving the Parent, everything must get rolled back. Flushing the child immediately brakes this principle.

            To be more specific. Parent's save method is the only point where I want the data to be flushed.



            Child c=new Child();
            c.setParent(activeParent);
            persist(c);
            childList.refresh();
            



            The above code assumes that the persist() method is an extension of an EntityHome. You could do away with the childList.refresh() call if you @Override the ChildQuery.refresh method and add an @Observer to receive the org.jboss.seam.afterTransactionSuccess.Child event callback.


            The persist I use is actually a EntityHome.getEntityManager().persist(c).

            This is because I'm adding a child on  ParentHome component. It seems that this one does not fire the org.jboss.seam.afterTransactionSuccess which is kinda logical.

            But when I use the a ChildeHome.persist, child gets flushed immediately and of course that is something I don't want. After the data is flushed, childList.refresh() works anyway, so I don't see why I would need the @Observer.

            Perhaps I just don't understand it's behavior correctly. I would appreciate some more info on your suggestion.