Hibernate 笔记 之 二 查询
1.Hibernate查询
1.1oid查询 即get
1 Users users = session.get(Users.class, 1);
1.2对象属性导航查询
1 //对象属性导航查询 2 @Test 3 public void obj() { 4 Session session = HibernateUtils.getCurrentSession(); 5 Transaction tx = session.beginTransaction(); 6 String name = session.get(Users.class,1) 7 .getClassroom().getClass_name(); 8 System.out.println(name); 9 tx.commit(); 10 }
1.3HQL
1 public class Hql { 2 /* 3 * hql是hibernate独创的 4 * hql语句中不会出现数据库的字段 5 * hql是面向对象查询 6 * hql使用的类名是全类名 7 * 如果项目中没有重复类名的实体那么使用直接用类名 8 */ 9 @Test 10 //简单查询 11 public void simpleHql() { 12 Session session = HibernateUtils.getCurrentSession(); 13 Transaction tx = session.beginTransaction(); 14 String hql="from Users"; 15 Query query = session.createQuery(hql); 16 @SuppressWarnings("unchecked") 17 List<Users> users = query.list(); 18 for (Users user : users) { 19 System.out.println(user); 20 } 21 tx.commit(); 22 } 23 24 @Test 25 //指定条件查询 26 public void parameterHql() { 27 Session session = HibernateUtils.getCurrentSession(); 28 session.beginTransaction(); 29 String hql = "from Users where id=?"; 30 Query query = session.createQuery(hql); 31 query.setParameter(0, 2); 32 Users user = (Users) query.uniqueResult(); 33 System.out.println(user); 34 } 35 36 @Test 37 //指定条件查询 38 public void parameter2Hql() { 39 Session session = HibernateUtils.getCurrentSession(); 40 session.beginTransaction(); 41 String hql = "from Users where id=:id"; 42 Query query = session.createQuery(hql); 43 query.setParameter("id", 2); 44 Users user = (Users) query.uniqueResult(); 45 System.out.println(user); 46 } 47 @SuppressWarnings("unchecked") 48 @Test 49 //分页 50 public void limitHql() { 51 Session session = HibernateUtils.getCurrentSession(); 52 session.beginTransaction(); 53 String hql = "from Users"; 54 Query query = session.createQuery(hql); 55 query.setFirstResult(1); 56 query.setMaxResults(1); 57 List<Users> user = query.list(); 58 System.out.println(user); 59 } 60 }
1 public class hql { 2 /* 3 * 在hql语句中在inner join 后带fetch的话是 4 * 迫切查询 5 * 迫切查询是查询返回是from后面的对象 6 * 而不用的时候返回的是object数组 7 */ 8 @Test 9 //manyTomany 10 public void hql1() { 11 Session session = HibernateUtils.getCurrentSession(); 12 Transaction tx = session.beginTransaction(); 13 String hql = "from Users u inner join fetch u.course "; 14 Query query = session.createQuery(hql); 15 List<Users> lists = query.list(); 16 for (Users users : lists) { 17 System.out.println(users.getCourse()); 18 } 19 // System.out.println(lists); 20 tx.commit(); 21 } 22 @Test 23 //oneTomany 24 public void hql2() { 25 Session session = HibernateUtils.getCurrentSession(); 26 Transaction tx = session.beginTransaction(); 27 String hql = "from Users u inner join u.classroom"; 28 Query query = session.createQuery(hql); 29 List<Object[]> list = query.list(); 30 /*for (Users users : list) { 31 System.out.println(users); 32 }*/ 33 tx.commit(); 34 } 35 }
1.4Criteria
1 public class CriteriaDemo { 2 3 @Test 4 //简单查询 5 public void simpleCriteria() { 6 Session session = HibernateUtils.getCurrentSession(); 7 Transaction tx = session.beginTransaction(); 8 Criteria criteria = session.createCriteria(Users.class); 9 List<Users> list = criteria.list(); 10 for (Users user : list) { 11 System.out.println(user); 12 } 13 tx.commit(); 14 } 15 @Test 16 //带参数查询 17 public void parameterCriteria() { 18 Session session = HibernateUtils.getCurrentSession(); 19 Transaction tx = session.beginTransaction(); 20 @SuppressWarnings("unchecked") 21 List<Users> list = session.createCriteria(Users.class) 22 .add(Restrictions.eq("id", 2)) 23 .list(); 24 for (Users users : list) { 25 System.out.println(users); 26 } 27 tx.commit(); 28 } 29 30 @Test 31 //使用聚集函数 32 public void parameter2Criteria() { 33 Session session = HibernateUtils.getCurrentSession(); 34 Transaction tx = session.beginTransaction(); 35 Criteria criteria = session.createCriteria(Users.class); 36 criteria.setProjection(Projections.rowCount()); 37 Long result = (Long) criteria.uniqueResult(); 38 System.out.println(result); 39 tx.commit(); 40 41 } 42 @Test 43 //分页 44 public void limitCriteria() { 45 46 Session session = HibernateUtils.getCurrentSession(); 47 Transaction tx = session.beginTransaction(); 48 Criteria criteria = session.createCriteria(Users.class); 49 criteria.setFirstResult(0); 50 criteria.setMaxResults(1); 51 List<Users> list = criteria.list(); 52 for (Users users : list) { 53 System.out.println(users); 54 } 55 tx.commit(); 56 } 57 @Test 58 /* 59 * 分离的criteria 60 * 在没有session对象的方法中封装条件 61 * 通过方法传参的方式将分离的criteria 62 * 传递到数据库控制层 63 */ 64 public void detached() { 65 DetachedCriteria criteria = DetachedCriteria.forClass(Users.class); 66 criteria.add(Restrictions.idEq(1)); 67 //----------------------------------------------------------- 68 Session session = HibernateUtils.getCurrentSession(); 69 Transaction tx = session.beginTransaction(); 70 Users result = (Users) criteria.getExecutableCriteria(session).uniqueResult(); 71 tx.commit(); 72 System.out.println(result); 73 } 74 }