"拼串"形成的HQL语句的写法,其能够形成一条语句,从而效率得到最大的提升。
用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);
//都要用事务否则不删除
用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);
//都要用事务否则不删除