select count(c) from Company c;
select count(distinct c.creationDate) from Company c;
Integer count = (Integer)session.find("select count(distinct c) from Company c").get(0);

String query = "from Company c join fetch c.departments where c.name = :name";
Query q = session.createQuery(query);
q.setParameter("name", "alibaba");
Company c = (Company)q.list().get(0);
Set departments = c.getDepartments();
session.close();

select elements(c.departments) from Company c where c.name = :name;

select elements(d) from Company c join c.departments d where c.name = :name;

from Company c join c.departments d;

使用左连接:from Company c left join c.departments d;
            from Company c left join c.departments d with d.name='技术部';
           
使用右连接:
            from Company c right join c.departments d where c.name='alibaba';
           
FETCH连接
            from Company c join fetch c.departments;
          和
            from Company c join c.departments d
生成的sql语句几乎一样,但是Hibernate执行该HQL得到的结果集不同

FETCH连接在以下几种情况下不能使用:
   (1):不能使用Query.iterate()来获得结果集
   (2):不能在分页方法中使用
   (3):不能带with条件
 
from Company c where c.departments.name='技术部';

在实际应用中,出于性能和消除外键关联的考虑,通常不会在映射文件中将两个具有关联关系的对象映射起来,但是两个对象有相应的属性可以进行关系对应
此时可以使用交叉连接的方式:
    select c from Company c, Departments d where c.id = d.companyId and d.name='技术部';
    和
   from Company c where c.departments.name='技术部';
   查询的结果集相同
  
select d from Company c, Department d where d.company=c and c.name='alibaba' and d.name='技术部';

可以使用特殊属性size或者特殊函数size()测试一个集合的大小:
   select size(c.departments) from Company c where c.name='alibaba';
   或者:
   select c.departments.size from Company c where c.name='alibaba';
  
from Company c order by c.creationDate, c.name, c.url
from Company c order by c.creationDate asc, c.name desc, url asc;

select c.name as name, size(c.departments) as e from Company c group by c.name;
select c.name as name, size(c.departments) as e from Company c group by c.name having c.name='alibaba';

子查询:
from Company c where not exists (from Department d where d.company=c);

批量更新和删除
批量更新:
     String query = "update Company set name=:newname where name=:name";
     Query query = session.createQuery(query);
     query.setString("name", "alibaba");
     query.setString("newname", "alibaba.com");
     int rowCount = query.executeUpdate();
     session.close();
    
批量删除:
     String query = "delete from Company where name=:name");
     Query q = session.createQuery(query);
     query.setString("name", "alibaba");
     int rowCount = query.executeUpdate();
     session.close();