Example On Hibernate Delete Query
This is the program to Delete a row (Object) from the database, just like using delete query in the jdbc program..
Files required to execute this program..
- Product.java (My POJO class)
- Product.hbm.xml (Xml mapping file )
- hibernate.cfg.xml (Xml configuration file)
- ClientProgram.java(java file to write our hibernate logic)
Product.java (POJO)
1234567891011121314151617181920212223242526272829303132333435package str; public class Product{ private int productId; private String proName; private double price; public void setProductId(int productId) { this.productId = productId; } public int getProductId() { return productId; } public void setProName(String proName) { this.proName = proName; } public String getProName() { return proName; } public void setPrice(double price) { this.price = price; } public double getPrice() { return price; } }
Product.hbm.xml
1234567891011121314<?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.Product" table="products"> <id name="productId" column="pid" /> <property name="proName" column="pname" length="10"/> <property name="price"/> </class> </hibernate-mapping>
hibernate.cfg.xml
1234567891011121314151617181920<?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="Product.hbm.xml"></mapping> </session-factory> </hibernate-configuration>
ClientProgram.java
12345678910111213141516171819202122232425262728package str; import org.hibernate.*; import org.hibernate.cfg.*; public class ClientProgram { 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.load(Product.class,new Integer(103)); Product p=(Product)o; Transaction tx = session.beginTransaction();
session.delete(p);
System.out.println("Object Deleted successfully.....!!"); tx.commit(); session.close(); factory.close(); } }
As usual compile all .java programs and run ClientProgram.java to see the output
Note:
- To deleting the object( 1 row) form the database we need to call delete method in the session.
- In the hibernate we have only one method to delete an object from the database that is what i have shown you here..
post a comment