Search This Blog

Wednesday, 26 December 2012

Criteria API - the basics

Until now I have used HQL in all the posts we have seen. I decided to go ahead and try the Criteria API. With criteria you create your queries programmatically and not via Strings. In the HQL examples the simplest Query was the select * query.
Query q = session.createQuery("from Entity");
The same using Criteria would be:
public static void testSimplestSelect() {
    final Session session = sessionFactory.openSession();
    Criteria criteria = session.createCriteria(Entity.class);
    List<Entity> entities = criteria.list(); 
    System.out.println(entities);
}
The resultant query generated by Hibernate is :
Hibernate: 
    /* criteria query */ 
       select
        this_.ID as ID0_0_,
        this_.NAME as NAME0_0_,
        this_.DATE as DATE0_0_,
        this_.MASTER_ID as MASTER4_0_0_ 
    from
        ENTITY this_
Earlier we saw how a query on the Object class resulted in loading of records from all tables. I tried the same in the above code:
public static void fetchAll() {
    final Session session = sessionFactory.openSession();
    Criteria criteria = session.createCriteria(Object.class);
    List<Object> all = criteria.list(); 
    System.out.println(all);
}
The result -
Hibernate: 
    /* criteria query */ 
    select
        this_.ID as ID1_0_,
        this_.DATA as DATA1_0_ 
    from
        ENTITY_MASTER this_
Hibernate: 
    /* criteria query */ 
    select
        this_.ID as ID2_0_,
        this_.`KEY` as KEY2_2_0_,
        this_.ENTITY_ID as ENTITY3_2_0_ 
    from
        CHILD_ENTITY this_
Hibernate: 
    /* criteria query */ 
    select
        this_.ID as ID0_0_,
        this_.NAME as NAME0_0_,
        this_.DATE as DATE0_0_,
        this_.MASTER_ID as MASTER4_0_0_ 
    from
        ENTITY this_
3 separate queries to load the data from three different tables. The above techniques employed working with a session instance to get the Criteria object created and running. There is also something famously known (especially to Spring users) as the DetachedCriteria.
From the java docs :
Some applications need to create criteria queries in "detached" mode,
where the Hibernate session is not available. This class may be 
instantiated anywhere, and then a Criteria may be obtained by passing 
a session to getExecutableCriteria(). All methods have the same 
semantics and behavior as the corresponding methods of the Criteria interface.
public static void fetchViaDetached() {
    DetachedCriteria detachedCriteria = DetachedCriteria.forClass(Entity.class);

    //afterwards when u have the session
    final Session session = sessionFactory.openSession();        
    Criteria criteria = detachedCriteria.getExecutableCriteria(session);
    System.out.println(criteria.list());
}
When working with the HibernateTemplate we pass the detached criteria instance to the template, which then generates the actual Criteria using the hibernate session and executes the criteria.

No comments:

Post a Comment