How to delete by criteria in hibernate?

For deleting use HQL which is the best option, I think, Criteria’s main purpose is for only retrieving the data.
This one is with Criteria

  Student student = (Student ) session.createCriteria(Student.class)
                    .add(Restrictions.eq("classId", classId)).uniqueResult();
  session.delete(student);

And this one is simple HQL query:

String hql = "delete from Student where classId= :classId";
session.createQuery(hql).setString("classId", classId).executeUpdate();

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)