Java Hibernate GetSession

时间:2018-07-19 10:39:11

标签: java hibernate criteria

我很难转换/最小化我拥有的这段非常长的代码(会话)。我每次需要会议时都在使用它。所以我的代码很冗长和混乱。请帮忙!如何最小化这个?谢谢!

    public List<Client> listOfClientInDonation(Map map) {
    String clientName = map.get("clientName") == null ? "" : (String) map.get("clientName");    

    List list = null;
    Transaction trns = null;
    Session sess = hibernateUtil.getSessionFactory().openSession();
    try {
        trns = sess.beginTransaction();
        Criteria crit = sess.createCriteria(Client.class);
      crit.add(Restrictions.like("name", clientName, MatchMode.ANYWHERE));
      crit.addOrder(Order.asc("name"));
      list = crit.list();
      sess.getTransaction().commit();
    } catch (RuntimeException e) {
        if (trns != null) {
            trns.rollback();
        }
        e.printStackTrace();
    } finally {
        sess.flush();
        sess.close();
    }
    return list;
}

0 个答案:

没有答案