Search This Blog

Friday, 14 September 2012

HQL - Getting all the records in the database

We have been dealing with Hibernate objects that represents table records. In the inheritance posts we saw how to fetch records by referring to the base class. This would result in all sub class objects being fetched. In Java all classes extend the Object class.
So what would the impact of this query be?
public static void testCompleteSelect() {
    final Session session = sessionFactory.openSession();
    Query q = session.createQuery("from java.lang.Object");
    List<Entity> entities = q.list(); //the cast did not even complain
    System.out.println(entities);
//but a run time cast in a for each will most definitely
//    for (Entity entity : entities) {
//        System.out.println(entity);
//    }
        
}
The logs are as below:
3547 [main] DEBUG org.hibernate.impl.SessionImpl  - opened session at timestamp:
 13410454990
3563 [main] DEBUG org.hibernate.engine.query.QueryPlanCache  - unable to locate 
HQL query plan in cache; generating (from java.lang.Object)
3656 [main] DEBUG org.hibernate.hql.ast.QueryTranslatorImpl  - parse() - HQL: fr
om com.model.Master
...
3922 [main] DEBUG org.hibernate.hql.ast.QueryTranslatorImpl  - parse() - HQL: fr
om com.model.Child
...
3985 [main] DEBUG org.hibernate.hql.ast.QueryTranslatorImpl  - HQL: from com.mod
el.Entity
...
4063 [main] DEBUG org.hibernate.jdbc.ConnectionManager  - opening JDBC connectio
n
...
4063 [main] DEBUG org.hibernate.SQL  - 
    /* 
from
    java.lang.Object */ 
    select
        master0_.ID as ID1_,
        master0_.DATA as DATA1_ 
    from
        ENTITY_MASTER master0_
Hibernate: 
    /* 
...
4141 [main] DEBUG org.hibernate.loader.Loader  - done processing result set (1 r
ows)
...
4125 [main] DEBUG org.hibernate.SQL  - 
    /* 
from
    java.lang.Object */ 
    select
        child0_.ID as ID2_,
        child0_.`KEY` as KEY2_2_,
        child0_.ENTITY_ID as ENTITY3_2_ 
    from
        CHILD_ENTITY child0_
Hibernate: 
    /* 
...
4146 [main] DEBUG org.hibernate.loader.Loader  - done processing result set (1 r
ows)
...
4188 [main] DEBUG org.hibernate.SQL  - 
    /* 
from
    java.lang.Object */ 
    select
        entity0_.ID as ID0_,
        entity0_.NAME as NAME0_,
        entity0_.DATE as DATE0_,
        entity0_.MASTER_ID as MASTER4_0_ 
    from
        ENTITY entity0_
Hibernate: 
    /* 
...
4203 [main] DEBUG org.hibernate.loader.Loader  - done processing result set (1 r
ows)
...
4219 [main] DEBUG org.hibernate.jdbc.ConnectionManager  - aggressively releasing
 JDBC connection
...
[[Master] : ( id 1 , data : master No 1 )], [Child] : ( id 1 , key : 1001 , pare
nt.Id : 1 )], [Entity] : ( id 1 , data : newOne , master.Id : 1 , date : null )]
]
The logs lead to the below observations:
  1. The Hibernate Engine generated a separate select query for each of the mapped class.
  2. It opened a single connection to the database for processing the code.
  3. For the three select queries, it used three prepared statements and also three result sets to generate the final result - a list of all the objects in the database.
  4. If the commented code was executed it would have resulted in a cast exception as the list was of a specific type.

No comments:

Post a Comment