Newsletter |
Hibernate One to Many Mapping Delete Query Example
Hibernate » on Jul 22, 2011 { 6 Comments } By Sivateja
Let us see the logic for hibernate one to many mapping delete query, Actually every thing is same like Hibernate One-to-Many Mapping Insert but only change is in OurLogic.java file.
But mates, ensure you came through these sessions for better understand
Files required..
- Vendor.java [pojo class]
- Customer.java [pojo class]
- OurLogic.java
- Customer.hbm.xml
- hibernate.cfg.xml
- Vendor.hbm.xml
Vendor.java
package str; import java.util.Set; public class Vendor { private int vendorId; private String vendorName; private Set children; public int getVendorId() { return vendorId; } public void setVendorId(int vendorId) { this.vendorId = vendorId; } public String getVendorName() { return vendorName; } public void setVendorName(String vendorName) { this.vendorName = vendorName; } public Set getChildren() { return children; } public void setChildren(Set children) { this.children = children; } }
Customer.java
package str; public class Customer { private int customerId; private String customerName; private int forevenId; public int getCustomerId() { return customerId; } public void setCustomerId(int customerId) { this.customerId = customerId; } public String getCustomerName() { return customerName; } public void setCustomerName(String customerName) { this.customerName = customerName; } public int getForevenId() { return forevenId; } public void setForevenId(int forevenId) { this.forevenId = forevenId; } }
Customer.hbm.xml
<?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping> <class name="str.Customer" table="customer"> <id name="customerId" column="custid" /> <property name="customerName" column="custname" length="10"/> <property name="forevenId" column="forevenid" insert="false" /> </class> </hibernate-mapping>
Vendor.hbm.xml
<?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping> <class name="str.Vendor" table="vendor"> <id name="vendorId" column="vendid" /> <property name="vendorName" column="vendname" length="10"/> <set name="children" cascade="all" > <key column="forevenid" /> <one-to-many class="str.Customer" /> </set> </class> </hibernate-mapping>
hibernate.cfg.xml
<?xml version='1.0' encoding='UTF-8'?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <property name="connection.driver_class">oracle.jdbc.driver.OracleDriver </property> <property name="connection.url">jdbc:oracle:thin:@www.java4s.com:1521:XE</property> <property name="connection.username">system</property> <property name="connection.password">admin</property> <property name="dialect">org.hibernate.dialect.OracleDialect</property> <property name="show_sql">true</property> <property name="hbm2ddl.auto">update</property> <mapping resource="Customer.hbm.xml"></mapping> <mapping resource="Vendor.hbm.xml"></mapping> </session-factory> </hibernate-configuration>
Deleting Single Parent Object With All Child
OurLogic.java
package str; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.Transaction; import org.hibernate.cfg.Configuration; public class OurLogic { public static void main(String args[]) { Configuration cfg = new Configuration(); cfg.configure("hibernate.cfg.xml"); SessionFactory factory = cfg.buildSessionFactory(); Session session = factory.openSession(); Object o = session.get(Vendor.class, new Integer(101)); Vendor v = (Vendor)o; Transaction tx = session.beginTransaction(); session.delete(v); tx.commit(); session.close(); System.out.println("One To Many is Done for deleting..!!"); factory.close(); } }
If we want to delete all the parent objects will all its corresponding child objects., then the logic will be like..
Deleting All Parent Objects With All Childs
OurLogic.java
package str; import java.util.Iterator; import java.util.List; import org.hibernate.Query; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.Transaction; import org.hibernate.cfg.Configuration; public class OurLogic { public static void main(String args[]) { Configuration cfg = new Configuration(); cfg.configure("hibernate.cfg.xml"); SessionFactory factory = cfg.buildSessionFactory(); Session session = factory.openSession(); Query qry =session.createQuery("from Vendor v"); List l=qry.list(); Iterator it = l.iterator(); Transaction tx = session.beginTransaction(); while(it.hasNext()) { Object o = it.next(); Vendor v = (Vendor) o; session.delete(v); } tx.commit(); session.close(); System.out.println("One To Many is Done for deleting all parents with childs...!"); factory.close(); } }
You Might Also Like
::. About the Author .:: | ||
Comments
6 Responses to “Hibernate One to Many Mapping Delete Query Example”
can you please give detailed tables on other relations also .
Hi,
Very nice tutorial .Thanks alot.
Nice tutorial
shall we use …..
createQuery(“delete from Vendor v”); instead of above query…
pls reply… 🙂
createQuery is used to perform the non select operations oly..
Shall we use …..
createQuery(“delete from Vendor v”); instead of above query…
pls reply… 🙂