예제 #1
0
  public void add(User user) {
    Session session = MySessionFactory.getSessionFactory().openSession();
    session.beginTransaction();

    session.save(user);

    session.getTransaction().commit();

    session.close();
  }
예제 #2
0
  @SuppressWarnings("unchecked")
  public List<User> queryAllUser() {

    List<User> users = new ArrayList<User>();
    Session session = null;
    try {
      session = MySessionFactory.getSessionFactory().openSession();
      session.beginTransaction();

      users = session.createQuery("from User").setCacheable(true).list();

      session.getTransaction().commit();

    } catch (Exception e) {
      e.printStackTrace();
    } finally {
      session.close();
    }

    return users;
  }