hibernate的查询方法以及返回的值类型

Hibernate的查询分几种 
HQL,QBC(QBE),SQL 

HQL查询 
HQL是基于面向对象的,里面没有表,也没有列,有类和属性 
直接以from开头,表示全部选择 
从Hibernate3.1可以执行修改和删除 

from Emp 

eg1.  带?参数查询 
Session session = HibernateSessionFactory.getSession(); 
String hql="from Emp e where e.ename like ?"; 
Query query=session.createQuery(hql); 
query.setString(0, "%"+"a"+"%"); 
List list=query.list(); 
System.out.println(list.size()); 
HibernateSessionFactory.closeSession(); 

eg.2 带:命名参数 
Session session = HibernateSessionFactory.getSession();    
String hql="from Emp e where e.ename like :name"; 
Query query=session.createQuery(hql); 
query.setString("name", "%a%"); 
List list=query.list(); 
System.out.println(list.size()); 
HibernateSessionFactory.closeSession(); 

eg3.in查询 
Session session = HibernateSessionFactory.getSession(); 
String hql="from Emp e where e.empno in (:list)"; 
Query query=session.createQuery(hql); 
query.setParameterList("list", new Long[]{1L,3L,5L}); 
List list=query.list(); 
System.out.println(list.size()); 
HibernateSessionFactory.closeSession(); 

eg4.连接查询 
Session session = HibernateSessionFactory.getSession(); 
String hql = "from Emp e where e.dept.dname like ?"; 
Query query = session.createQuery(hql); 
query.setString(0, "%RE%"); 
List list = query.list(); 
System.out.println(list.size()); 
HibernateSessionFactory.closeSession(); 

eg5. 笛卡尔积 
Session session = HibernateSessionFactory.getSession(); 
String hql = "select e from Emp e,Dept"; 
Query query = session.createQuery(hql); 
List list = query.list(); 
System.out.println(list.size()); 
HibernateSessionFactory.closeSession(); 

eg6. 带条件 
Session session = HibernateSessionFactory.getSession(); 
String hql = "select e from Emp e,Dept d where e.dept=d"; 
Query query = session.createQuery(hql); 
List list = query.list(); 
System.out.println(list.size()); 
HibernateSessionFactory.closeSession(); 

eg7. select 多个对象,会形成 List里面放Object数组,数组里面放对象 
Session session = HibernateSessionFactory.getSession(); 
String hql = "select e,d from Emp e,Dept d where e.dept=d"; 
Query query = session.createQuery(hql); 
List list = query.list(); 
System.out.println(list.size()); 
HibernateSessionFactory.closeSession(); 

eg8. select 一列,就会形成这一列类型的集合,不会生成Object数组 
Session session = HibernateSessionFactory.getSession(); 
String hql = "select e.empno from Emp e,Dept d where e.dept=d"; 
Query query = session.createQuery(hql); 
List list = query.list(); 
System.out.println(list.size()); 
HibernateSessionFactory.closeSession(); 

eg9. select多个值,会形成Object[] 
Session session = HibernateSessionFactory.getSession(); 
String hql = "select e.empno,e.ename from Emp e,Dept d where e.dept=d"; 
Query query = session.createQuery(hql); 
List list = query.list(); 
System.out.println(list.size()); 
HibernateSessionFactory.closeSession(); 

eg10.查询生成List里面放HashMap,key由as后面的字符串决定 
Session session = HibernateSessionFactory.getSession(); 
String hql = "select new map(e.empno as id,e.ename as name) from Emp e"; 
Query query = session.createQuery(hql); 
List list = query.list(); 
System.out.println(list.size()); 
HibernateSessionFactory.closeSession(); 

eg11.迫切左连接查询 
Session session = HibernateSessionFactory.getSession(); 
String hql = "from Emp e left join fetch e.dept"; 
Query query = session.createQuery(hql); 
List list = query.list(); 
System.out.println(list.size()); 
HibernateSessionFactory.closeSession(); 

eg12.分页查询 
Session session = HibernateSessionFactory.getSession(); 
String hql = "from Emp "; 
Query query = session.createQuery(hql); 
query.setFirstResult(10);//从第几条开始,初始是0  (page-1)*pageSize; 
query.setMaxResults(5);//取几条 
List list = query.list(); 
System.out.println(list.size()); 

HibernateSessionFactory.closeSession();


更多信息请查看  hibernate教程 http://www.itchm.com/forum-61-1.html

你可能感兴趣的:(Hibernate,session,list,String,object,query)