hibernate增删改查的标准范例(二)

2014-11-24 11:10:29 · 作者: · 浏览: 2
Session session = getSession();
tx = session.beginTransaction();
session.update(entity);
tx.commit();
} catch (Exception e) {
tx.rollback();
throw new RuntimeException("更新错误"+e);
} finally {
HibernateSessionFactory.closeSession();
}
}
public Admin getAllObjects(String name) {
try {
return (Admin) getSession().createQuery("from Admin a where a.adminName = :name")
.setParameter("name", name).uniqueResult();
} catch (Exception e) {
throw new RuntimeException("根据用户查询有错误"+e);
} finally {
HibernateSessionFactory.closeSession();
}
}
public Admin login(Admin entity) {
try {
return (Admin) getSession().createQuery("from Admin a where a.adminName = :name and a.adminPassword = :pass ").setString("name",entity.getAdminName()).setString("pass", entity.getAdminPassword()).uniqueResult();
} catch (Exception e) {
throw new RuntimeException("登录错误"+e);
} finally {
HibernateSessionFactory.closeSession();
}
}
}