我是休眠准则的新手,我的目标是使用更少的代码来编写具有准则的函数。
我怎么能做到这一点?
public List<Test> listOfTest(Map map) {
String testName = map.get("testName") == null ? "" : (String) map.get("testName");
List list = null;
Transaction trns = null;
Session sess = hibernateUtil.getSessionFactory().openSession();
try {
trns = sess.beginTransaction();
Criteria crit = sess.createCriteria(Test.class);
crit.add(Restrictions.like("testName", testName, MatchMode.ANYWHERE));
crit.addOrder(Order.asc("testName"));
list = crit.list();
sess.getTransaction().commit();
} catch (RuntimeException e) {
if (trns != null) {
trns.rollback();
}
e.printStackTrace();
} finally {
sess.flush();
sess.close();
}
return list;
}
对此:
public List<Test> listOfTest(Map map) {
String testName = map.get("testName") == null ? "" : (String) map.get("testName");
Criteria criteria = getSession().createCriteria(Test.class);
crit.add(Restrictions.like("testName", testName, MatchMode.ANYWHERE));
crit.addOrder(Order.asc("testName"));}
return list ;
}