0 Replies Latest reply on Aug 21, 2009 4:22 PM by shadowmask22

    Relationship(child set) lost after parent is updated

    shadowmask22

      Hi,
      I am working on an application using seam 2.1.2 , deployed on Apache Tomcat 6.0.2 and mySql 5.0.67. I have 2 models called Branch and Supplier , with a one-to-many relationship between Branch and Supplier .




      public class Branch { 
      ............................
      private Set<Supplier> suppliers = new HashSet<Supplier>(0);
      ............................
      @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "branch")
              public Set<Supplier> getSuppliers() {
                      return this.suppliers;
              }
      
              public void setSuppliers(Set<Supplier> suppliers) {
                      this.suppliers = suppliers;
              }
      }
      
      public class Supplier {
      .............................
      private Branch branch;
      ..............................
      @ManyToOne(fetch = FetchType.LAZY)
              @JoinColumn(name = "branch_id")
              @NotNull
              public Branch getBranch() {
                      return this.Branch;
              }
      
              public void setBranch(Branch branch) {
                      this.branch = branch;
              }
      }





      The DAO's that I am using :-


      To save supplier -




      public void save(Supplier supplier) {
                      UserTransaction tx = Transaction.instance();
                      try {
                              if(!tx.isActiveOrMarkedRollback()){
                                      tx.begin();
                              }
                              log.info("supplier id before persist : "+supplier.getId());
                              entityManager.persist(supplier);
                              log.info("supplier id after persist : "+supplier.getId());
                              entityManager.flush();
                              log.info("supplier id after flush : "+supplier.getId());
                              tx.commit();
                              tx.begin();
                      } catch (SystemException e) {
                              // TODO Auto-generated catch block
                              e.printStackTrace();
                      } catch (NotSupportedException e) {
                              // TODO Auto-generated catch block
                              e.printStackTrace();
                      } catch (SecurityException e) {
                              // TODO Auto-generated catch block
                              e.printStackTrace();
                      } catch (IllegalStateException e) {
                              // TODO Auto-generated catch block
                              e.printStackTrace();
                      } catch (RollbackException e) {
                              // TODO Auto-generated catch block
                              e.printStackTrace();
                      } catch (HeuristicMixedException e) {
                              // TODO Auto-generated catch block
                              e.printStackTrace();
                      } catch (HeuristicRollbackException e) {
                              // TODO Auto-generated catch block
                              e.printStackTrace();
                      }
              }





      To update the Branch :-



      public void update(PartPrice partPrice) {
                      log.info("Before merge : Size of supplier set : "+partPrice.getSuppliers().size());
                      entityManager.merge(partPrice);
                      log.info("Before flush , after merge : Size of supplier set : "+partPrice.getSuppliers().size());
                      entityManager.flush();
                      log.info("After flush : Size of supplier set : "+partPrice.getSuppliers().size());
              }
      




      Other information that could be of significance :-
      1.Both the models have an overridden equals and hashcode method
      2.The Dao's are Application Scoped
      3.I am using DTO for capturing Data from the form , and then saving/deleting/updating it


      My Problem :


      1. A supplier is created and saved , for a Branch that has no suppliers as of now . This is of significance as , the problem is not encountered if the Branch has at least one supplier .


      2. After the supplier is persisted (it is visible in the db) , I call a function to update the associated Branch . As you can see in my Dao for updating Branch  , I am displaying the size of the supplier set before and after the update . The problem is that , as soon as the update happens , the supplier set for that Branch becomes empty . I cannot see the suppliers for that Branch on the webpage , or on my console .


      3. If I keep adding more suppliers , the same behavior is observed . If , I do a refresh on the webpage , I can see the supplier list , and any supplier which is subsequently added . As I said before , this problem occurs only when there are no suppliers for a branch , and I add a new one .


      So where am I going wrong ?