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
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.
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.
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.
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:
Criteria.setFetchMode()
in a Hibernate Criteria queryFETCH
keyword in a HQL queryA 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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With