Hibernate query method (HQL/QBC/QBE) Summary, hqlqbc

Source: Internet
Author: User

Hibernate query method (HQL/QBC/QBE) Summary, hqlqbc

As an old ORM framework, Hibernate has made significant contributions to promoting the database persistence layer.

It provides more and more data query methods, from SQL to self-built HQL, To object-oriented standardized query.

Although the query method is a little dazzling, the configuration is a little complicated to use.

But it cannot cover up its charm at all. This blog attempts to summarize all the query methods of Hibernate.

Radish and vegetables have their own love. You can select one of them based on your preferences during work.

1. HQL mode. The parameter uses Question Mark placeholder (obsolete in version 4.1)

public List<UserPO> getUserList(UserPO userPO) {        String hql = "from UserPO where name = ? and passwd= ?";        Query query = getHibernateSession().createQuery(hql);        query.setParameter(0, userPO.getName());        query.setParameter(1, userPO.getPasswd());        return query.list();    }

2. HQL mode. The parameter uses Named placeholder

   public List<UserPO> getUserList(UserPO userPO) {        String hql = "from UserPO where name = :userName and passwd= :userPwd";        Query query = getHibernateSession().createQuery(hql);        query.setParameter("userName", userPO.getName());        query.setParameter("userPwd", userPO.getPasswd());        return query.list();    }

3. HQL mode. The parameters use JPA placeholders.

  public List<UserPO> getUserList(UserPO userPO) {        String hql = "from UserPO where name = ?1 and passwd= ?2";        Query query = getHibernateSession().createQuery(hql);        query.setParameter("1", userPO.getName());        query.setParameter("2", userPO.getPasswd());        return query.list();    }

4. HQL mode. parameters are bound to objects.

   public List<UserPO> getUserList(UserPO userPO) {        String hql = "from UserPO where name = :name and passwd= :passwd";        Query query = getHibernateSession().createQuery(hql);        query.setProperties(userPO);        return query.list();    }

PS: note that the placeholder name parameter must be consistent with the PO attribute, and only the placeholder name can be used.

Of course, HQL can be adjusted to SQL for the above-mentioned 1--4 filling query method, which is the same.

5. QBC (Query By Criteria) method. The Restrictions object is used for parameters.

   public List<UserPO> getUserList(UserPO userPO) {        Criteria criteria = getHibernateSession().createCriteria(UserPO.class);        criteria.add(Restrictions.eq("name",userPO.getName()));        criteria.add(Restrictions.eq("passwd",userPO.getPasswd()));        return criteria.list();    }

6. QBC (Query By Criteria) mode. The parameters are bound to objects.

  public List<UserPO> getUserList(UserPO userPO) {        Criteria criteria = getHibernateSession().createCriteria(UserPO.class);        criteria.add(Example.create(userPO));        return criteria.list();    }

PS: QBC (Query By Criteria) method. The key abstract objects used include:
Restrictions --> set query Restrictions
Order --> set query sorting Conditions
Projections --> tool class methods for statistics and grouping.

The above 5-6 is also my favorite method, with the Object-Oriented Query Method to cater to object-oriented programming.

7. offline Condition Query

 public List<UserPO> getUserList(UserPO userPO) {        DetachedCriteria detachedCriteria = DetachedCriteria.forClass(UserPO.class);        detachedCriteria.add(Restrictions.eq("name",userPO.getName()));        detachedCriteria.add(Restrictions.eq("passwd",userPO.getPasswd()));        Criteria criteria = detachedCriteria.getExecutableCriteria(getHibernateSession());        return criteria.list();    }

The benefits of offline query: You can pass the query object DetachedCriteria as a parameter to the DAO layer to reduce the DAO Layer Code.

8. QBC (Query By Criteria)

   public List<UserPO> getUserList(UserPO userPO) {        Criteria criteria = getSession().createCriteria(UserPO.class);        criteria.setFirstResult(4);        criteria.setMaxResults(3);        return criteria.list();    }}

PS: The paging query mainly specifies two parameters (starting from what and how many ):
SetFirstResult () and setMaxResults () of the Query or Criteria object ()

Of course, the Criteria object in 8 can also be converted into a Query object, using the HQL or SQL Query method.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.