I have an object –
@Entity
public class myObject{
public string fieldA;
public string fieldB
...
}
I don’t have any instance of it but I want to delete all the rows in db that have
fieldA == A.
Is this the correct way ?
try {
session.beginTransaction();
session.createQuery("delete from MyObject where fieldA = " +
"BlaBla").executeUpdate();
session.getTransaction().commit();
savedSuccessfully = true;
} catch (HibernateException e) {
session.getTransaction().rollback();
savedSuccessfully = false;
} finally {
session.close();
}
return savedSuccessfully;
Take a look at using native sql in hibernate and the
session.createSQLQueryto make sure hibernate doesn’t get involved with wiring beans with HSQL.Also avoid using String concatenation to build your queries. Lastly, your database will most like be your bottle neck with a simple query like this so make sure you have proper indexes on the table.