Fetching strategy in hibernate






 what  are  fetching  strategy  in Hibernate?  


To optimize   the   Hibernate select statement, efficiently   Hibernate defines fetching strategies
Hibernate has four type of fetching strategies.

1. fetch-“join” = Disable the lazy loading, always load all the collections and entities.

2. fetch-“select” (default) = Lazy load all the collections and entities.
3. batch-size=”N” = Fetching up to ‘N’ collections or entities, *Not record*.
4. fetch-“subselect” = Group its collection into a sub select statement.


  Here is   example how to declare   fetching   strategies. In  (**.hbm.xml ) file   and
 annotation.


Mapping   based   declaration

<hibernate-mapping>
    <class name="com.bksoft.emp" table="stock">
        <set name="empattndsRecords"  cascade="all" inverse="true"
            table="empattnds_record" batch-size="10" fetch="select">
            <key>
                <column name="emp_id" not-null="true" />
            </key>
            <one-to-many class="com.bksoft.common.empRecords" />
        </set>
    </class>
</hibernate-mapping>

  

 Annotation based declaration..

 @Entity
@Table(name = "emp", catalog = "xyz")
public class Stock implements Serializable{
...
                @OneToMany(fetch = FetchType.LAZY, mappedBy = "empRecords")
                @Cascade(CascadeType.ALL)
                @Fetch(FetchMode.SELECT)
               @BatchSize(size = 10)
                public Set< empattndsRecords > getEmpAttendenceRecords() {
                                return EmpAttendenceRecords();
                }

Fetching strategy in hibernate Fetching  strategy  in  hibernate Reviewed by Mukesh Jha on 2:51 AM Rating: 5

No comments:

Add your comment

All Right Reserved To Mukesh Jha.. Theme images by Jason Morrow. Powered by Blogger.