-->
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.  [ 4 posts ] 
Author Message
 Post subject: using saveOrUpdate Method
PostPosted: Wed Jun 09, 2010 8:51 am 
Newbie

Joined: Wed Jun 09, 2010 8:29 am
Posts: 3
Hi Team,

I have a specific requirement in my application which is built in Flex and Java with Spring and hibernate.Below is my requirement:

1)There are two tables in the database Category and Product with one to many relationship between them i.e a Category has multiple products.

Category Table columns:
    CategoryId
    Category Name

Product Table Columns:
    ProductId
    Product Name
    CategoryId-->Foreign key from Category table.

2)The User interface which displays Categories and the related products for each Category.There is an option to delete or add a new product to a category in the screen.

3)When the user deletes a product for a category in the user interface do I have to explicitly call a delete method to delete a product from Product table

4) Is there any way where I can remove the Product Object from the list of Products in Category bean and use SaveOrUpdate to save Category bean and the entry is automatically deleted from product table as it is not in the list of products for that category.

Could someone please let me know how to solve this issue.

Thanks,
Pappu.


Top
 Profile  
 
 Post subject: Re: using saveOrUpdate Method
PostPosted: Wed Jun 09, 2010 10:13 am 
Beginner
Beginner

Joined: Wed Jul 01, 2009 8:11 am
Posts: 34
yes this is possible through cascading. look here for how this works: http://www.cs.bham.ac.uk/~aps/syllabi/2 ... ml#cascade

If you need to know more basic stuff i advise to buy a Hibernate book. I possess Hibernate in Action which i think is good. Cascading for example is nicely explained there.


Top
 Profile  
 
 Post subject: Re: using saveOrUpdate Method
PostPosted: Wed Jun 09, 2010 11:12 am 
Newbie

Joined: Wed Jun 09, 2010 8:29 am
Posts: 3
Thank you very much for the update.

Currently my cascade option is set to all.I shall try other options like delete-orphan and all-delete-orphan and check it works.

Thanks,
Pappu.


Top
 Profile  
 
 Post subject: Re: using saveOrUpdate Method
PostPosted: Fri Jun 11, 2010 3:52 am 
Newbie

Joined: Wed Jun 09, 2010 8:29 am
Posts: 3
Hi Garz,

Looks like this is not working.Below is my the code I have used.

Category.hbm.xml
Code:
<hibernate-mapping>
    <class name="Category" table="category" catalog="deletepoc">
        <id name="id" type="integer">
            <column name="ID" />
            <generator class="identity" />
        </id>
        <property name="name" type="string">
            <column name="NAME" length="100" />
        </property>
        <set name="products" inverse="true" cascade="delete-orphan" lazy="false">
            <key>
                <column name="CATID" />
            </key>
            <one-to-many class="Product" />
        </set>
    </class>
</hibernate-mapping>


Products.hbm.xml
Code:
<hibernate-mapping>
    <class name="Product" table="product" catalog="deletepoc">
        <id name="id" type="integer">
            <column name="ID" />
            <generator class="identity" />
        </id>
        <many-to-one name="category" class="Category" fetch="select">
            <column name="CATID" />
        </many-to-one>
        <property name="prodName" type="string">
            <column name="PROD_NAME" length="100" />
        </property>
    </class>
</hibernate-mapping>


Delete Method:
Code:
   public void deleteOrphans(){
      Category category=new Category();
                category.setId(new Integer(1));
      category.setName("CatName_updated");
               
      Product prod=new Product();
      prod.setProdName("New Product1");
      prod.setCategory(category);
      
      Product prod1=new Product();
      prod1.setProdName("New Product2");
      prod1.setCategory(category);

      category.getProducts().add(prod1);
      category.getProducts().add(prod);
      
      this.catDao.attachDirty(category);
   }


DAO Method:
Code:
   public void attachDirty(Category instance) {
      log.debug("attaching dirty Category instance");
      try {
         getHibernateTemplate().saveOrUpdate(instance);
         log.debug("attach successful");
      } catch (RuntimeException re) {
         log.error("attach failed", re);
         throw re;
      }
   }


Before I call the deleteOrphans() method there are other products for the Category Id:1 in products table.I want to delete these products from the Products table and add the new products(New Product1 and New Product2) in one call to the delete orphans method.

Could you please help me to solve this issue.

Thanks,
Pappu.


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

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.