hibernate增删改查的标准范例(二)
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) {
} 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();
}
}
}