Some queries regarding fetch strategies in hibernate and relation of fetchtype with fetchmode?
Asked Answered
P

2

5

I have gone thru some of the links for hibernate fetch strategies on net.One brief and concise link i liked is http://www.mkyong.com/hibernate/hibernate-fetching-strategies-examples/. We have four type of fetch strategies. these are :-

  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.

My first question which one of the above qualifies for eager or lazyloading fetch type?

To raise my queries about hibernate fetch strategies the i am considering below code snippet in my Department class

  @OneToMany(mappedBy = "department", cascade = CascadeType.ALL, fetch = FetchType.EAGER,      orphanRemoval = true)
 @Fetch(value = FetchMode.SELECT)
 @BatchSize(size = 10)
 private Set<EmployeeData> employees = new HashSet<EmployeeData>();

As per my understanding As soon as i mention fetchtype as eager, i am left only with join fetch strategy but when i mention as fetchtype as lazyloading, i have other three options i.e select,batch-size and subselect.Agreed? Now if i look at code snippet in one my legacy project , it mentioned fetch type as eager and fetch strategy as select which contracdicts each other. Right?

Another query is i do not see batch-size option when i write FetchMode. and do control + space in eclipse though i see other three fetch strategies?

Pyelonephritis answered 23/1, 2012 at 10:58 Comment(0)
L
16

Hibernate collections have fetch type and fetch mode settings.

Fetch type specifies when elements of collection are retrieved, and fetch mode specifies how Hibernate retrieves them.

So, FetchMode.SELECT and FetchMode.SUBSELECT are legal with both FetchType.EAGER and FetchType.LAZY. The difference is that with FetchType.EAGER an additional select query is executed immediately, whereas with FetchType.LAZY it's executed after the first access to the collection.

FetchMode.JOIN, however, doesn't make sense with FetchType.LAZY.

Batch size is an additional optimization for FetchMode.SELECT, so that it should be configured by its own annotation (@BatchSize) and has nothing to do with FetchMode enumeration itself.

See also:

Lindley answered 23/1, 2012 at 11:39 Comment(1)
Amazing and quick clarificationPyelonephritis
N
0

The batch-size=N is almost equal to the fetch=subselect. Let me explain you how does it so:

Suppose you have 10000 parent records and each one of them have 500 child records, and if you use the fetching strategy as subselect then this is how the queries would look like:

Subselect:
Select * from parent;
select * from child where child.Parent_Id in(select Parent_Id from Parent).

Note: Just in case if the IN clause exceeds the limit which is provided by the underlying data base, obviously the above query fails. Here is the situation where the batch-size coming into the picture. By using batch-size we can mention number of Parent_Id's to be participated in the IN clause.

Select & batch-size:
Select * from parent;
select * from child where child.Parent_Id in(1,2,3,4......till the IN clause limit of the underlying DB).
select * from child where child.Parent_Id in(1001,1002......till the IN clause limit of the underlying DB).

Note: batch-size should be used with fetch=select, but not with fetch=subselect(since it doesn't make any sense).

Neutral answered 23/1, 2012 at 10:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.