package com.bjsxt.hibernate;
import java.util.Iterator;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
import org.logicalcobwebs.asm.tree.TryCatchBlockNode;
import junit.framework.TestCase;
public class QueryTest extends TestCase {
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.beginTransaction().commit();
} catch (Exception e) {
e.printStackTrace();
session.beginTransaction().rollback();
} finally {
HibernateUtils.closeSession(session);
}
}
}
转载于:https://my.oschina.net/zlp5201/blog/404213