Hibernate-- offline query

1, Criteria query:

(1) General method:

 

Disadvantages: Each query method dao layers need to write the corresponding offline query can solve this problem. 

(2) offline:

 

 2, off-line query

DetachedCriteria used to construct the query conditions, then the method call parameters as DetachedCriteria to the business layer objects.

        DetachedCriteria detachedCriteria=DetachedCriteria.forClass(Student.class);
        detachedCriteria.add(Restrictions.eq("sex",""));

After obtaining the DetachedCriteria business layer objects, can be configured directly in the session scope Criteria, query, the following code may be common to a plurality of target objects.

 public static void testSel() {
   
        Session session = HibernateUtils.openSession();
        Transaction transaction = session.beginTransaction();
        Criteria criteria=detachedCriteria.getExecutableCriteria(session);
        List list=criteria.list();
        System.out.println(list);
        transaction.commit();
        session.close();
    }

 

 

Guess you like

Origin www.cnblogs.com/zhai1997/p/11979836.html
Recommended