看视频中的代码有一点疑问,Transaction tx=null;为空也可以tx.commit();吗?
//事务对象
Transaction tx=null;
String hql="";
try{
Session session=MyHibernateSessionFactory.getSessionFactory().getCurrentSession();
hql="select * from USER where username=? and password=?";
Query query=session.createQuery(hql);
query.setParameter(9, u.getUsername());
query.setParameter(1, u.getPassword());
List list=query.list();
tx.commit();
if(list.size()>0){
return true;
}else{
return false;
}
}catch(Exception e){
e.printStackTrace();
return false;
}
finally{
if(tx!=null){
tx=null;
}
}