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)
package 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
<?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
<?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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
package 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
Eclipse output
Oracle DB, Before Run The Program
Oracle DB, After Run the program
- 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..
No comments:
Post a Comment