Next, delete a table row with the Hibernate API. As an example, delete the table row for edition "March-April 2005"
. Create a HQL query which selects a database table row to delete.
String hqlQuery="from Catalog as catalog where catalog.edition='March-April 2005'";
Open a database session with the openSession()
method of the SessionFactory
object:
Session sess = sessionFactory.openSession();
Create a Query
object with the HQL query. Obtain the result set List
for the HQL query. Obtain the result set item to be deleted:
Query query = sess.createQuery(hqlQuery); List list = query.list(); Catalog catalog = (Catalog) list.get(0);
Begin a session transaction using the beginTransaction()
method:
Transaction tx = sess.beginTransaction();
Delete the row specified in the HQL query with the delete()
method of the Session
object and commit the transaction:
sess.delete(catalog); tx.commit();
The example Java application, HibernateDB.java
, has the addToCatalog
method to add data, retrieveFromCatalog...