[SXT][WY]Hibernate03 Query

public void testQuery() {
  Session session = null;
  try {
   session = HibernateUtils.getSession();
   session.beginTransaction();
   Query query = session.createQuery("from User");
   query.setFirstResult(2);
   query.setMaxResults(2);
   List userList = query.list();
   for (Iterator iter=userList.iterator(); iter.hasNext();) {
    User user = (User)iter.next();
    System.out.println(user.getId());
    System.out.println(user.getName());
   }
   session.getTransaction().commit();
  }catch(Exception e) {
   e.printStackTrace();
   session.getTransaction().rollback();
  }finally {
   HibernateUtils.closeSession(session);
  }
 }

你可能感兴趣的:(Hibernate)