Search This Blog

Friday 22 June 2012

Hibernate And Bulk Operations -2

I the previous post we saw bulk updates via Hibernate. We can also perform bulk deletes and bulk inserts through Hibernate.
Bulk Deletes
static void testBulkDelete() {
    Session session = sessionFactory.openSession();
    Transaction transaction = null;
    try {
        transaction = session.beginTransaction();
        Entity entity = (Entity) session.load(Entity.class, 3L);
        System.out.println("Entity name is " + entity.getName());
        System.out.println("Deleting all entities");
        Query query = session.createQuery("delete Entity");
        query.executeUpdate();
        transaction.commit();
        System.out.println("Name of Entity with id " + entity.getId() 
                + " is " + entity.getName() );
        session.refresh(entity);
        System.out.println("Is entity still there ? " + entity);
        session.close();
    } catch (Exception e) {
        e.printStackTrace();
        transaction.rollback();    
    }
}
I again fetched an entity from the table(id =3). Then I executed the bulk delete code that deletes all records from the table. Unlike the previous example, I added a refresh call that attempts to fetch the entity again from the database. The logs indicate the results:
2657 [main] DEBUG org.hibernate.SQL  - 
    select
        entity0_.ID as ID0_0_,
        entity0_.NAME as NAME0_0_ 
    from
        ENTITY entity0_ 
    where
        entity0_.ID=?
Entity name is as
Deleting all entities 
3157 [main] DEBUG org.hibernate.engine.query.HQLQueryPlan  - HQL param location 
recognition took 0 mills (delete Entity)
3204 [main] DEBUG org.hibernate.SQL  - 
    delete 
    from
    ENTITY 
3219 [main] DEBUG org.hibernate.impl.SessionImpl  - after transaction completion
Name of Entity with id 3 is as
3250 [main] DEBUG org.hibernate.SQL  - 
     select
        entity0_.ID as ID0_0_,
        entity0_.NAME as NAME0_0_ 
    from
        ENTITY entity0_ 
    where
        entity0_.ID=?
3204 [main] DEBUG org.hibernate.jdbc.AbstractBatcher  - preparing statement
org.hibernate.UnresolvableObjectException: No row with the given identifier exis
ts: [com.bulk.Entity#3]
    at org.hibernate.UnresolvableObjectException.throwIfNull(UnresolvableObjectExce
ption.java:42)
    at org.hibernate.event.def.DefaultRefreshEventListener.onRefresh(DefaultRefresh
EventListener.java:126)
    at org.hibernate.event.def.DefaultRefreshEventListener.onRefresh(DefaultRefresh
EventListener.java:39)
As Hibernate did not find the entity on refresh it threw an exception.

Bulk Inserts
Hibernate also allows us to insert records directly.
static void testBulkInsert() {
    Session session = sessionFactory.openSession();
    Transaction transaction = null;
    try {
        transaction = session.beginTransaction();
        System.out.println("Inserting entities...");
        Query query = session.createQuery("Insert into Entity(name)" +
                " select e.name from Entity e where e.id <5");
        query.executeUpdate();
        transaction.commit();
        session.close();
    } catch (Exception e) {
        e.printStackTrace();
        transaction.rollback();    
    }
}
The query selects the records based on the inner HQL query and then inserts them directly into the database. The logs indicate the insert operation:
2687 [main] DEBUG org.hibernate.jdbc.JDBCContext  - after transaction begin
Inserting entities...
2719 [main] DEBUG org.hibernate.engine.query.QueryPlanCache  - unable to locate 
HQL query plan in cache; generating (Insert into Entity(name) select e.name from
 Entity e where e.id <5)
...
3203 [main] DEBUG org.hibernate.SQL  - 
    insert 
    into
        ENTITY
        ( NAME ) select
            entity0_.NAME as col_0_0_ 
        from
            ENTITY entity0_ 
        where
            entity0_.ID<5

No comments:

Post a Comment