Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delete all objects from database in hibernate Spring java

I want to delete all those rows from xyz table where id = 1 using hibernate spring.

I have tried following code but its not giving error but not deleting rows -

 Session session = (Session) getEm().getDelegate();
 String sql ="Delete from xyz where id=:id" ;
SQLQuery query  = session.createSQLQuery(sql);
query.setParameter("id", "1");
int flg = query.executeUpdate();

Can you please help me to delete all rows using hibernate query.

like image 445
Geetanjali Jain Avatar asked Jul 05 '26 19:07

Geetanjali Jain


1 Answers

Try wrapping your code within a transaction like this:

Session session = (Session) getEm().getDelegate();
Transaction tx = session.beginTransaction();
String sql ="Delete from xyz where id=:id" ;
SQLQuery query  = session.createSQLQuery(sql);
query.setParameter("id", "1");
int flg = query.executeUpdate();
tx.commit();
like image 85
Arthur Noseda Avatar answered Jul 08 '26 16:07

Arthur Noseda



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!