Query和Criteria接口都提供执行查询语句,并且返回查询结果的方法,返回结果有两种类型,在先前的学习中,
使用的都是返回list,输出结果还得循环,有时候我们只想查询一条数据,没有必要让查询结果返回一个集合,
这个时候,返回一个对象就好。
针对返回集合,Query和Criteria提供list()方法,集合中存放满足条件的持久化对象。
针对返回单个对象,Query和Criteria提供uniqueResult()方法,返回单个满足条件的持久化对象。
返回单个对象的逻辑:
(1)使用setMaxResults(1)设置最多检出条数
(2)使用uniqueResult()返回一个Object类型对象,再强制转换成你想要的类型
HQL检索单个对象实例:
package com.lanhuigu.hibernate.test; import org.hibernate.Query; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.Transaction; import org.hibernate.cfg.Configuration; import com.lanhuigu.hibernate.entity.Customer; public class TestHQLUnique { public static void main(String[] args){ Configuration cfg = new Configuration().configure(); SessionFactory sessionFactory = cfg.buildSessionFactory(); Session session = sessionFactory.openSession(); Transaction tr = session.beginTransaction(); //1.创建Query Query query = session.createQuery("from Customer order by name desc "); //2.设置最大检出条数 query.setMaxResults(1); //3.执行sql,返回一个Object对象,强制转换为Customer对象 Customer customer = (Customer) query.uniqueResult(); //4.输出结果 System.out.println(customer.getName()); //5.事务提交 tr.commit(); //6.关闭session session.close(); } }控制台输出结果:
Hibernate: select this_.ID as ID1_0_0_, this_.NAME as NAME2_0_0_, this_.EMAIL as EMAIL3_0_0_, this_.PASSWORD as PASSWORD4_0_0_, this_.PHONE as PHONE5_0_0_, this_.ADDRESS as ADDRESS6_0_0_, this_.SEX as SEX7_0_0_, this_.IS_MARRIED as IS8_0_0_, this_.DESCRIPTION as DESCRIPT9_0_0_, this_.IMAGE as IMAGE10_0_0_, this_.BIRTHDAY as BIRTHDA11_0_0_, this_.REGISTERED_TIME as REGISTE12_0_0_, this_.HOME_PROVINCE as HOME13_0_0_, this_.HOME_CITY as HOME14_0_0_, this_.HOME_STREET as HOME15_0_0_, this_.HOME_ZIPCODE as HOME16_0_0_, this_.COMP_PROVINCE as COMP17_0_0_, this_.COMP_CITY as COMP18_0_0_, this_.COMP_STREET as COMP19_0_0_, this_.COMP_ZIPCODE as COMP20_0_0_ from CUSTOMERS this_ order by this_.NAME desc limit ? test
QBC检索单个对象实例:
package com.lanhuigu.hibernate.test; import org.hibernate.Criteria; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.Transaction; import org.hibernate.cfg.Configuration; import org.hibernate.criterion.Order; import com.lanhuigu.hibernate.entity.Customer; public class TestQBCUnique { public static void main(String[] args) throws Exception{ Configuration cfg = new Configuration().configure(); SessionFactory sessionFactory = cfg.buildSessionFactory(); Session session = sessionFactory.openSession(); Transaction tr = session.beginTransaction(); //1.创建Criteria对象 Criteria criteria = session.createCriteria(Customer.class); //2.排序 //criteria.addOrder(Order.asc("name"));//升序 criteria.addOrder(Order.desc("name"));//降序 //3.设置最大检出数据条数 criteria.setMaxResults(1);//最多检出的条数 //4.执行SQL,返回一个Object对象,强制转换为Customer对象 Customer customer = (Customer) criteria.uniqueResult(); //5.输出结果 System.out.println(customer.getName()); //6.事务提交 tr.commit(); //7.关闭session session.close(); } }
控制台输出结果:
Hibernate: select this_.ID as ID1_0_0_, this_.NAME as NAME2_0_0_, this_.EMAIL as EMAIL3_0_0_, this_.PASSWORD as PASSWORD4_0_0_, this_.PHONE as PHONE5_0_0_, this_.ADDRESS as ADDRESS6_0_0_, this_.SEX as SEX7_0_0_, this_.IS_MARRIED as IS8_0_0_, this_.DESCRIPTION as DESCRIPT9_0_0_, this_.IMAGE as IMAGE10_0_0_, this_.BIRTHDAY as BIRTHDA11_0_0_, this_.REGISTERED_TIME as REGISTE12_0_0_, this_.HOME_PROVINCE as HOME13_0_0_, this_.HOME_CITY as HOME14_0_0_, this_.HOME_STREET as HOME15_0_0_, this_.HOME_ZIPCODE as HOME16_0_0_, this_.COMP_PROVINCE as COMP17_0_0_, this_.COMP_CITY as COMP18_0_0_, this_.COMP_STREET as COMP19_0_0_, this_.COMP_ZIPCODE as COMP20_0_0_ from CUSTOMERS this_ order by this_.NAME desc limit ? test