学习了一下hibernate的hql查询语句,,,貌似挺好用的...
有点象sql,,还能用聚合函数avg,sum之类的
.setFirstResult(2).setMaxResults(2) //可以组合使用,以实现分页功能
package com.memorycat.hibernatetest.hql; import java.util.Iterator; import java.util.List; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.Transaction; import org.hibernate.cfg.Configuration; import org.junit.AfterClass; import org.junit.BeforeClass; import org.junit.Test; import com.memorycat.hibernatetest.model.User; public class HqlTest { static Session session; static Transaction transaction; @BeforeClass public static void setUpBeforeClass() throws Exception { Configuration configuration = new Configuration().configure(); SessionFactory sessionFactory = configuration.buildSessionFactory(); session = sessionFactory.openSession(); transaction = session.beginTransaction(); } @AfterClass public static void tearDownAfterClass() throws Exception { transaction.commit(); session.close(); } @Test public void testSave() { User user = new User(0, "a", "b"); for (int i = 0; i < 3; i++) session.save(user); } @Test public void testQuery() { List list = session.createQuery("select sum(u.id) from User u where u.name=:name") .setString("name", "a")/*.setFirstResult(2).setMaxResults(2)*/ .list(); for (Iterator iterator = list.iterator(); iterator.hasNext();) { System.out.println(iterator.next()); } } }