hibernate中用hql怎么执行delete的sql语句

如题所述

你可以使用一楼的方式
如果使用hql方式,参考如下:
public void testDml(){
Session session = null;
Transaction tx = null;
try {
session = HibernateUtils.getSession();
tx = session.getTransaction();
session.beginTransaction();
Query query = session.createQuery("delete Student s where s.id=?");
query.setInteger(0, 1);
query.executeUpdate();
tx.commit();
} catch (HibernateException e) {
tx.rollback();
e.printStackTrace();
}finally{
HibernateUtils.closeSession(session);
}
}
温馨提示:答案为网友推荐,仅供参考