4 Replies Latest reply on Oct 10, 2009 10:26 PM by first last

    EntityHome.refresh() does not reload a field (which is a collection)

    first last Newbie

      Hey y'all,


      I am struggling with the following persistence issue for a while now:


      I have a Client entity that has a collection of Representatives:




      @Entity
      @Name("client")
      public class Client implements Serializable {
              ...
              private List<Representative> representatives =
                       new ArrayList<Representative>();
      
              @OneToMany(cascade = {CascadeType.PERSIST, CascadeType.MERGE, CascadeType.REMOVE},
                           mappedBy="client")
           @org.hibernate.annotations.Cascade(value=
                                   {org.hibernate.annotations.CascadeType.DELETE_ORPHAN})
           public List<Representative> getRepresentatives() {
                return representatives;
           }
           public void setRepresentatives(List<Representative> representatives) {
                this.representatives = representatives;
           }
              ...
      }



      The Representative class is nothing special:




      @Entity
      @Name("representative")
      public class Representative implements Serializable {
              ...
              private Client client;
              @NotNull
           @ManyToOne
           public Client getClient() {
                return client;
           }
           public void setClient(Client client) {
                this.client = client;
           }
              ...
      }




      For controlling a Client entity, I have a ClientHome class, and in it - a method to refresh the Client from the DB (if the user clicks an 'undo' button while filling out the form) :




      @Name("clientHome")
      public class ClientHome extends EntityHome<Client>
      {
              ...     
           public void undo() {
                super.getEntityManager().refresh(super.getInstance());
           }
              ...
      }






      Now, I have a ClientDetails.xhtml page that shows details of the client (duh) including its list of representatives. (The page has a page-parameter for the client Id, as is the base use case of using EntityHome):




      ...
      <rich:dataTable id="repTable"
                value="#{clientHome.instance.representatives}"
                var="_rep"
           <rich:column>
                      <h:outputText value="#{_rep.name}" />
           </rich:column>
      ...
      



      You can also click on a Representative and edit his details, the entire thing runs in a long-running conversation which has flush-mode="manual", and it works great, EXCEPT that when you click the 'undo' button:




      <h:commandButton id="undo"
                     value="UNDO"
                     action="#{clientHome.undo}"
                     immediate="true" />



      Whatever changes were made to the client's representatives still appear (!) The 'representatives' field of the Client entity is not being properly reloaded from the DB.
      That is so even though the query does happen and get logged out:




      21:50:44,531 INFO  [STDOUT] Hibernate: 
          select
              client0_.id as id37_0_,
              client0_.name as name37_0_,
              ...
          from
              Client client0_ 
          where
              client0_.id=?
      
      21:50:44,609 INFO  [STDOUT] Hibernate: 
          select
              representa0_.client_id as client14_1_,
              representa0_.id as id1_,
              representa0_.id as id43_0_,
              representa0_.name as name43_0_,
              ...
          from
              Representative representa0_ 
          where
              representa0_.client_id=?




      Any idea why the dirty Representative remain intact in the Persistence Context, and are not replaced by they second query's results ??


      Thanks for any info!