Search This Blog

Saturday 30 June 2012

Intercepting Events in Hibernate

Much of what Hibernate does, happens in the background and the application is not aware of the same. For example dirty checking of entities and scheduling sql update queries for the modified objects is something that Hibernate manages on its own.This is very good in a way, because it removes the amount of code we need to write.

Thursday 28 June 2012

Data Filters in Hibernate

I think everybody has at some point come across views in Database.Views generally represent a subset of the data contained in a table. A common scenario is one wherein we use soft deletion . For example instead of actually deleting an entity, we have a flag column in the row which we set as true indicating the entity has been deleted. We then create a view to access all the "alive" records.
Views thus provide us a filtered access of data in the table.(This is only one of its uses.) Hibernate provides something similar called dynamic views.

Monday 25 June 2012

Stateless Session

In an earlier post I used the ScrollableResults object along with a session to batch multiple operations. In this case I had to manually flush and clear the session after every n SQL operations to prevent my persistence cache from having a memory overflow.
Hibernate also offers an alternative type of Session where it counts on the user to mange the SQL updates on his own.

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;

Tuesday 5 June 2012

Hibernate And Bulk Operations -1

We have often executed SQL queries of the type:
update <table> set <record_column> = <value> where <condition>;
These kind of queries that results in multiple records being updated are termed bulk updates.Hibernate allows us to do the same too. Consider the below code that updates the name column of all Entity records

Saturday 2 June 2012

Hibernate And Batch Operations

There are often scenarios in our application wherein we need to perform batch updates. For example if there was a need to update the zip code of all records in Address table a batch update would be a very good way to achieve the same. A single update SQL query would ensure that the job is done at the database level.
However there arise complications in big applications wherein the update operation would be much more complex then a singe update query or the code complexity better handled in java then in a stored procedure.