hibernate清空表
- 作者: 狗族长老
- 来源: 51数据库
- 2020-10-04
用Query删除
Session session=HibernateSessionFactory.getSession();
String hql="delete People where id=:id";
Transaction t=null;
try
{
t=session.beginTransaction();
Query q=session.createQuery(hql);
q.setParameter("id", id);
q.executeUpdate();
t.commit();
}catch(Exception ex)
{
if(t!=null)
{
t.rollback();
}
}finally
{
session.close();
}
直接用Session
People p=null;
Session session=HibernateSessionFactory.getSession();
p=(People)session.get(People.class, id);
if(p!=null)
session.delete(people);
//都要用事务否则不删除
推荐阅读
