Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

hibernate, to be lazy or not to be lazy? [closed]

I have entity A, which has a many-to-many relation to entity B.

So the table layout is : A, AB(mapping table), B

To get an object of entity A: I call A.getById() which does getHibernateTemplate().get(A.class, id) using spring and hibernate.

The problem is, sometimes ensuing code will just need A, sometimes ensuing code will continue to access the associated B's, so we'd like to use lazy loading in some cases and eager in some other cases. but the problem is that all database access is provided through the same single ADao.java, so there is only one method getById().

Should I create two versions of method getById()?

But then for more complex cases, if A is also attached to C through many-to-many, then there could be variants of lazy-loading-C and eager-loading-C,so the required getById() variants quickly grows exponentially.

what is your opinion on this choice?

Thanks

like image 495
teddy teddy Avatar asked Dec 13 '11 17:12

teddy teddy


People also ask

Is lazy loading default in Hibernate?

1. Working with lazy associations. By default, Hibernate uses lazy select fetching for collections and lazy proxy fetching for single-valued associations. These defaults make sense for most associations in the majority of applications.

What is difference between lazy true and lazy false in Hibernate?

To use lazy collection, you may optionally use lazy="true" attribute in your collection. It is by default true, so you don't need to do this. If you set it to false, all the child objects will be loaded initially which will decrease performance in case of big data.

Why Hibernate is lazy loading?

Hibernate now can "lazy-load" the children, which means that it does not actually load all the children when loading the parent. Instead, it loads them when requested to do so. You can either request this explicitly or, and this is far more common, hibernate will load them automatically when you try to access a child.


1 Answers

For general considerations take a look at the Hibernate 3.6 docs about fetching strategies. The default fetching strategy is defined in mapping annotations or in a hbm.xml file. There are three ways to dynamically switch from a default lazy-loading strategy to an eager-loading strategy. The first two require separate implementations of DAO methods for lazy-loading and eager-loading use cases:

  1. Criteria.setFetchMode() in a Hibernate Criteria query
  2. FETCH keyword in a HQL query
  3. Since Hibernate 3.5 (not quite sure now, maybe 3.6) there is the third option of using fetch profiles to dynamically switch from lazy-loading to eager-loading.

A fetch profile is enabled/disabled on a session scope. So, provided the desired fetch profile is set in the current session, you can use the same DAO method for a lazy-loading as well as for an eager-loading use case.

The important thing to be aware of here is that you can only switch from a lazy-loading strategy defined in annotations or in a hbm.xml file to an eager-loading strategy and not vice versa. This restriction is independent of the method used for switching the fetching strategy.

like image 66
tscho Avatar answered Sep 23 '22 16:09

tscho