Friday, July 31, 2009

@BatchSize() Annotation in hibernate

While searching on the net on how to gain perfomance in JPA/Hibernate i stumbled upon an article written on hibernate annotation batchsize and it intrigued me on how it really works. After reading on and going through the algorythm defined by Hibernated on how it works i understood that this adds a sufficient perfomance gain in the event of you having large collections within your entities.

 --------Updated on 07/30/2012 according to the clarification given by Jeremy---------

For example lets consider an Airport parent containing a collection of airlines as such;

    @OneToMany()
    List<Airlines> airlines = new ArrayList<Airlines>();
Imagine you had such a statement in one of your entities. Assume there can be 100 Airport objects at any given time.Without the BatchSize annotation, Hibernate will first retrieve the Airports separately and for each airport it will retrieve the airlines separately. Now consider the following code snippet;
    @OneToMany
    @BatchSize(size=16)
   List<Airlines> airlines = new ArrayList<Airlines>();
When you use the batch size annotation what hibernate does is it divides the number of elements that would come in the resulting query by the batch size defined. In this case its 100 / 16 so you get 6 and remainder 4. What this implies is hibernate will go to the database 6 times to fetch 16 Airport objects' with their Airlines collection initialized and then go again another time to retrieve the remaining 4 Airports again with their Airlines collection initialized. So what the @BatchSize does is decide how many collections should be initialized.