hibernate怎么删除数据
展开全部
用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);
//都要用事务否则不删除
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);
//都要用事务否则不删除
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询