Hibernate HQL vs QBC查询

Hibernate HQL vs QBC查询
+++在映射文件中定义命名查询
<class .. >
</class>
<query name="findCustomersByName"><![CDATA[
        from Customer c where c.name like :name
]]></query>
语句:Query q=session.getNamedQuery("findCustomersByName");
        q.setString("name",name);
        List result=query.list();
++++ HQL Vs QBC 检索方式

比较运算
1    . 检索年龄大于18的Customer
        HQL    session.createQuery("from Customer cwhere c.age>18");
        QBC    Criteria criteria=session.createCriteria(Customer.class);
                    criteria.add(Expression.gt("age",18));
2 . 检索年龄不等于 18的
        HQL session.createQuery("from Customer c where c.age<>18")
        QBC Criteria criteria =session.createCriteria(Customer.class);
                criteria.add(Expression.not(Expression.eq("age",new Integer(18))));
3.    检索姓名为空的Customer对象
    HQL    session.createQuery("from Customer c where c.name is null");
    QBC    Criteria criteria =session.createCriteria(Customer.class);
                criteria.add(Expression.isNull("name"));
4. 检索不属于任何客户的订单
    HQL    session.createQuery("from Order o where o.customer is null");
    QBC    Criteria criteria =session.createCriteria(Order.class);
                criteria.add(Expression.isNull("customer"));
5. HQL    Query q=session.createQuery("from Customer c where lower(c.name)='tom'");
                Query q=session.createQuery("from Customer c where upper(c.name)='TOM'");
    QBC    Criteria criteria =session.createCriteria(Customer.class);
                criteria.add(Expression.eq("name","tom").ignoreCase());
范围运算
1. 检查姓名为 Tom,Mike 或者Jack 的 Customer对象
        HQL session.createQuery("from Customer c where c.name in('Tom','Mike','Jack')");
        QBC Criteria criteria=session.createCriteria(Customer.class);
                String names[]={"Tom","Mike","Jack"};
                criteria.add(Expression.in("name",names));
2. 检查年龄在 18 到    25之间的Customer对象
    HQL session.createQuery("from Customer c where c.age between 18 and 25");
    QBC Criteria criteria=session.createCriteria(Customer.class);
            criteria.add(Expression.between("age",new Integer(18),newInteger(25)))
3. 检索年龄不在 18到    25之间的Customer对象;
    HQL session.createQuery("from Customer c where c.age not between 18 and 25");
    QBC Criteria criteria=session.createCriteria(Customer.class);
            criteria.add(Expression.not(Expression.between("age",new Integer(18),newInteger(25))))
字符串模式匹配
1. 检索姓名以 "T"开头的Customer对象
        HQL    session.createQuery("from Customer cwhere c.name like 'T%'");
        QBC    Criteria criteria=session.createCriteria(Customer.class);
                    criteria.add(Expression.like("name","T%"));
                    %表示    任意个字符
                    _表示    一个字符
                    Or
                criteria.add(Expression.like("name","T",MatchMode.START));
            MatchMode 类中有:
                                            START        开头
                                            END            结尾
                                            ANYWHERE 包含
                                            EXACT        精确
逻辑运算符
    Expression.and(Expression1,Expression2);
    Expression.or(Expression1,Expression2);
   
连接查询:::::
1 . 映射文件中的配置
lazy="true" 延迟加载
fetch="select" 迫切加载
2.
    2.1    迫切左外连接, HQL : from Customer c left join fetch c.orders o where c.name like 'T'");
                                    QBC : criteria.setFetchMode("orders",FetchMode.EAGER); 迫切加载
        QBC 只支持 迫切左外连接,与内连接
    2.2    左外连接,        HQL : from Customer c left join c.orders where c.name like 'T%'
                                       
    2.3 内连接        HQl :    from Customer c inner join c.orders o where c.name like 'T%'
                   
   
2.4 迫切内连接 ,    HQL:    from customer c inner join fetch c.orders o where c.name like 'T%'
2.5 隐式内连接 , HQL :    from customer c where c.homeAddress.provice like '%hai'
2.6 右外连接    ,    HQL :    from customer right outer join c.orders o where c.name like 'T%'

报表查询 :
    select new mypack.CustomerRow(c.id,c.name,o.orderNumber)
    from customer c join c.orders o
    where o.orderNumber like 'T%'

    distinct 关键字 消除重复关键字
    order by    asc||desc 排序
    group by 列        分组查询

你可能感兴趣的:(Hibernate HQL vs QBC查询)