Search This Blog

Tuesday, 28 February 2012

I don't want to share my bean

In the previous example we saw how to set bean properties via the configuration. We can inject any value (or reference) into any property of the bean using the configuration files. However this leads to the side effect that the same bean reference may end up as a property among multiple beans.

Loading the Configuration

In our previous posts we created an XML based configuration file that provided the Bean Factory information on the beans to be created. The XmlBeanFactory class takes a resource instance as a part of its constructor parameter.

Saturday, 25 February 2012

Making A Detached Object Persistent Again

Hibernate allows detached objects to be associated with a persistence context again.One way to achieve this is to use the update method of the session.

Thursday, 23 February 2012

Controlling the size of the Persistence cache

As we saw earlier the Persistence Cache holds a collection of loaded persistent objects. For every object that we load in Hibernate a separate copy (called a snapshot) is maintained within the persistence cache. This copy is used by Hibernate to mange dirty checks.(It is also used as a first level cache.) A downside to this phenomenon is that if we load a very huge object graph into the session there is a possibility that we might run into an OutOfMemoryException.

Tuesday, 21 February 2012

Write-Behind Technique In Hibernate

Calling in the code does not cause an immediate SQL insert to be fired. Similarly  session.delete(entity) or session.update(entity) will not result immediately in the execution of the sql delete or update queries being fired.
When objects associated with the persistence context are modified, the changes are not immediately propagated to the database.

Sunday, 19 February 2012

First Level Cache and Repeatable Reads

As we saw earlier, with every hibernate session, a persistence context is associated. This context is aware of all the persistent objects that are associated with the hibernate session. The objects are maintained in maps with the identifier as the primary key. Hibernate very smartly uses this data to perform some query optimization.

Friday, 17 February 2012

Automatic Dirty Checking

Consider the below code which loads a simple Entity from the database and updates it.
public static void testUpdate() {
    Session session = sessionFactory.openSession();
    Transaction transaction = session.beginTransaction();

Thursday, 16 February 2012

The Hibernate Persistence Context

In our previous post we saw the object cycle and how the Hibernate session kept a tab on all the persistent objects. A call to session.toString() returns the following:
l.Entity#1]],collectionKeys=[]];ActionQueue[insertions=[] updates=[] deletions=[
] collectionCreations=[] collectionRemovals=[] collectionUpdates=[]])
There is something inside the Hibernate session called the PersistenceContext. What is it ?

Tuesday, 14 February 2012

The Hibernate Object Life-Cycle

The Hibernate framework is used to mange the persistence of objects. It allows objects to be associated with data present in the tables. It saves/modifies and inserts records in the database tables based on values in these objects and the operations performed on them using the Hibernate interfaces.
In the sequence of these operation calls, the data objects exist in various states. These states and the transition from one state to another forms the persistence life cycle.

Sunday, 12 February 2012

To Singleton or Not To Singleton

Consider the below bean definition in our spring configuration file
<bean id="pBean" class="com.performer.Singer"/>


In the previous post we saw how to create additional SQL using the <database-object> element. The queries were specified in the mapping file and Hibernate picked it from there and executed them as a part of the hbm2ddl operation.However Hibernate also allows us to programmatically control the generation of scripts.

Thursday, 9 February 2012

Auxiliary database objects

Hibernate's DDL export tool allows us to generate the SQL from our hbm mappings. However it is possible that we need to execute some additional sql that cannot be generated from the Hibernate mapping files. One way to do is by using the import.sql file. We can add all our triggers/procedures and other sql objects in this file and manage this file. Hibernate will execute this file as part of the database creation process.The other method is to create auxiliary database objects.

Tuesday, 7 February 2012

Identifying Beans - Name or Id

In the previous post we saw Spring and its relation to the term Beans. Now to look at beans from the implementation perspective. The first question is how to identify a Bean?
Beans in the xml file can be identified by their name or id property. For this post and a few others we shall use the below interface:

Monday, 6 February 2012

Creating sql indexes via Hibernate

The definition of a database index  from wikipedia is as follows:
A database index is a data structure that improves the speed of data retrieval operations on a database table at the cost of slower writes and increased storage space.

Saturday, 4 February 2012

Spring and Beans

Whenever I come across the word Spring, the word bean is inevitably around the corner. Is there are any strong relation between the two ? Is it a part of Java glossary ? Or is it just a terminology bandied about by the Spring team. What exactly is this bean ?

The Spring Container

In our previous post we saw how the BeanFactory was used. If the same code was to be written without spring

A Simple Spring Program

In this post, I shall attempt to create a simple bean using spring.

Sharp Turn in the Road

I have been learning Hibernate ( and  creating small examples) for quite some time now. And I am feeling a bit bored of this db thing now. I guess I need a change. So I decided to sharpen my skills at one another technology I have been using in the past year. Spring.
I have been using Spring in my project for the last year now. We started fresh on it with zero ground knowledge. At every stage of the development we have been amazed and impressed with what Spring has had to offer. So to bring a dash of change to the blog, I decided to take a stab at spring.
For my learning I had referred to a host of similar blogs, the Spring source site, my fellow workers, spring's source code and the book "Spring In Action- Second Edition". My knowledge might very well turn out to be a mix. (its definitely not something new, that I am putting out there ;) )Hope it helps.
This does not mean that Hibernate stops here. I shall continue with both Spring and Hibernate and at some point blog on a mix of the two. Till then cheers.

Friday, 3 February 2012

Creating Column, Table and Database level constraints

In earlier posts we saw how to use the hibernate hbm2ddl tool. We were able to create column constraints set unique column constraints and add not-null checks. In addition to those we saw before, Hibernate also allows creation of table level constraints such as foreign keys.