[ojb-users] Storing mapped objects
Brought to you by:
thma
From: Govindarajan S (Gover) <gov...@az...> - 2002-05-29 13:22:49
|
Hi, I have a doubt regarding the storing of mapped objects. I tried to implement the 1:1 relationship between the ProductGroup and Article. Whenever i was setting the product instance in the article and when i persist the article the ProductGroup is not persisted. Look at the code below, Article article = new Article(); article.setArticleName ("TestArticle"); ProductGroup prodGrp = new ProductGroup(); prodGrp.setProdGroupName("Group1"); prodGrp.setDescription ("Testgroup"); article.setProductGroup (prodGrp); try { broker.beginTransaction(); broker.store(article); broker.commitTransaction(); } catch (PersistenceBrokerException pExp) { System.out.println ("----- Error persisting Article -----"); pExp.printStackTrace(); } // end of try-catch There are two tables namely the ProductGroup and Article. And the mapping is all done as per the tutorial:3. With the above code, i expected the ProductGroup to be saved as well. But i had to persist the ProductGroup object ('prodGrp') separately. Is this a bug or iam missing out a funda here? This was the case with the 1:n mapping as well. I retrieve the ProductGroup and add articles to it. But i persist the ProductGroup hoping that the objects in the collection are also persisted. But i dont find the articles persisted. Code is below, Criteria qryCriteria = new Criteria(); qryCriteria.addEqualTo ("prodGroupName","Grocery"); QueryByCriteria criterion = new QueryByCriteria (ProductGroup.class, qryCriteria); ProductGroup prodGroup = (ProductGroup)broker. getObjectByQuery(criterion); Article art1 = new Article(); art1.setArticleName ("Tea"); art1.setProductGroup (prodGroup); Article art2 = new Article(); art2.setArticleName ("Coffee"); art2.setProductGroup (prodGroup); prodGroup.addArticle (art1); prodGroup.addArticle (art2); try { broker.beginTransaction(); broker.store(prodGroup); broker.commitTransaction(); System.out.println("--- Added the articles ---"); } catch (PersistenceBrokerException pExp) { System.out.println("---- Error storing 1:n relation objs ---"); pExp.printStackTrace(); } // end of try-catch any help in this regard would be greatly appreciated regds gover |