These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: Relationship(child set) lost after parent is updated
PostPosted: Fri Aug 21, 2009 10:24 am 
Newbie

Joined: Fri Aug 21, 2009 9:48 am
Posts: 1
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 .

Code:
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;
   }
}


Code:
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 -

Code:
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 :-

Code:
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 ?


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.