Hi Garz,
Looks like this is not working.Below is my the code I have used.
Category.hbm.xmlCode:
<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.xmlCode:
<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.