Hibernate API的使用(Query、Criteria、SQLQuery对象)
Query对象
我们使用Query对象不需要编写SQL语句,但是得写HQL语句。
HQL:Hibernate Query Language;Hibernate提供的查询语言,和SQL语句很相似。
HQL和SQL的区别:
使用SQL操作的是表和表字段;
使用HQL操作的是实体类和实体类的属性;
查询所有记录的HQL:from 实体类名称
Query对象的使用:
步骤一:创建Query对象;
步骤二:调用Query对象里面的方法得到结果;
代码演示:
@Test //使用Query对象 public void testQuery(){ SessionFactory sessionFactory = null; Session session = null; Transaction transaction = null; try { sessionFactory = HibernateUtil.getSessionFactory(); session = sessionFactory.openSession(); //开启事务 transaction = session.beginTransaction(); //1 创建Query对象 //编写HQL语句 Query query = session.createQuery("from User"); //2 调用Query对象里面的方法得到结果 List<User> userList = query.list(); for (User user : userList) { System.out.println(user); } //提交事务 transaction.commit(); } catch (Exception e) { // TODO: handle exception e.printStackTrace(); transaction.rollback(); }finally{ session.close(); sessionFactory.close(); } } |
Hibernate: select user0_.ID as ID1_0_, user0_.USERNAME as USERNAME2_0_, user0_.PASSWORD as PASSWORD3_0_, user0_.ADDRESS as ADDRESS4_0_ from USER user0_ User [id=1, username=yyyyyy, password=null, address=null] User [id=3, username=uc, password=up, address=tw] User [id=4, username=qqqqq, password=111111, address=shanxi] User [id=7, username=nihao111, password=nihaopwd111, address=U.S.A111]
|
Criteria对象
说明:使用这个对象也可以实现查询操作,但是使用这个对象的时候,不需要编写HQL语句,直接调用方法就可以了。
使用过程和Query很相似:
过程如下:
- 创建Criteria对象;
- 调用对象里面的方法得到结果;
代码演示:
//1 创建Criteria对象 Criteria criteria = session.createCriteria(User.class); //2 调用Query对象里面的方法得到结果 List<User> userList = criteria.list(); for (User user : userList) { System.out.println(user); } |
Hibernate: select this_.ID as ID1_0_0_, this_.USERNAME as USERNAME2_0_0_, this_.PASSWORD as PASSWORD3_0_0_, this_.ADDRESS as ADDRESS4_0_0_ from USER this_ User [id=1, username=yyyyyy, password=null, address=null] User [id=3, username=uc, password=up, address=tw] User [id=4, username=qqqqq, password=111111, address=shanxi] User [id=7, username=nihao111, password=nihaopwd111, address=U.S.A111] |
SQLQuery对象
使用Hibernate的时候,也可以调用底层SQL语句来实现。
实现过程:
1、创建SQLQuery对象;
2、调用对象的方法实现;
代码演示:
SQLQuery sqlQuery = session.createSQLQuery("select * from user"); //2 调用Query对象里面的方法得到结果 //返回的list集合,默认是数组结构 List<Object[]> userList = sqlQuery.list(); for (Object[] userObject : userList) { System.out.println(Arrays.toString(userObject)); } |
Hibernate: select * from user [1, yyyyyy, null, null] [3, uc, up, tw] [4, qqqqq, 111111, shanxi] [7, nihao111, nihaopwd111, U.S.A111] |
解决问题:返回list中的每部分是对象形式
//1 创建SQLQuery对象 SQLQuery sqlQuery = session.createSQLQuery("select * from user"); sqlQuery.addEntity(User.class); //2 调用Query对象里面的方法得到结果 //返回的list集合,默认是数组结构 List<User> userList = sqlQuery.list(); for (User userObject : userList) { System.out.println(userObject); } |
Hibernate: select * from user User [id=1, username=yyyyyy, password=null, address=null] User [id=3, username=uc, password=up, address=tw] User [id=4, username=qqqqq, password=111111, address=shanxi] User [id=7, username=nihao111, password=nihaopwd111, address=U.S.A111] |