Hibernate--Criteria

BtWangZhi發表於2017-09-25

1 執行SQL語句

    @SuppressWarnings({ "deprecation", "unchecked" })
    public void criteria02(){
        Session session=HibernateUtil.getSessionFactory().getCurrentSession();
        Transaction transaction=session.beginTransaction();

        Criteria criteria=session.createCriteria(User.class).add(Restrictions.sqlRestriction(" name like ?", "張%", Hibernate.STRING));
        List<User> lstUser=criteria.list();
        for (User user : lstUser) {
            System.out.println(user.getName()+"  "+user.getPassword());
        }
        transaction.commit();
    }

2 條件

    public void testQuery22(){
        Session session = HibernateUtil.getSession();
        Transaction tx = session.beginTransaction();

        List<Book> list =session.createCriteria(Book.class)
                .add(Restrictions.eq("id", 1))
                .list();
                for(Book b:list){
                    System.out.println(b+"-"+b.getCategory().getId());
                }
        tx.commit();
        HibernateUtil.closeSession();
    }

3 in

    public void testQuery23(){
        Session session = HibernateUtil.getSession();
        Transaction tx = session.beginTransaction();
        List<Integer> inl = new ArrayList<Integer>();
        inl.add(1);
        inl.add(3);
        List<Book> list =session.createCriteria(Book.class)
                .add(Restrictions.in("id", inl))
                .list();
                for(Book b:list){
                    System.out.println(b+"-"+b.getCategory().getId());
                }
        tx.commit();
        HibernateUtil.closeSession();
    }

3 like查詢

    public void testQuery24(){
        Session session = HibernateUtil.getSession();
        Transaction tx = session.beginTransaction();
        List<Book> list =session.createCriteria(Book.class)
                //.add(Restrictions.like("name", "中%"))
                //.add(Restrictions.like("name", "的", MatchMode.EXACT))
                //ilike ignoreCase like忽略大小寫
                .add(Restrictions.ilike("name", "%的%"))
                .list();
                for(Book b:list){
                    System.out.println(b+"-"+b.getCategory().getId());
                }
        tx.commit();
        HibernateUtil.closeSession();
    }

4 or 邏輯分組

    public void criteria01(){
        Session session=HibernateUtil.getSessionFactory().getCurrentSession();
        Transaction transaction=session.beginTransaction();
        Criteria criteria=session.createCriteria(User.class).add(Restrictions.like("name", "張%"));
        criteria.add(Restrictions.or(Restrictions.eq("id", 1),Restrictions.eq("name", "張三")));
        List<User> lstUser=criteria.list();
        for (User user : lstUser) {
            System.out.println(user.getName()+"  "+user.getPassword());
        }
        transaction.commit();
    }

5 排序

public void criteria02(){
        Session session=HibernateUtil.getSessionFactory().getCurrentSession();
        Transaction transaction=session.beginTransaction();
        Criteria criteria=session.createCriteria(User.class);
        criteria.addOrder(Order.desc("id"));
        List<User> lstUser=criteria.list();
        for (User user : lstUser) {
            System.out.println(user.getName());
        }
    }