I understand how to configure my model to fetch eager or lazy.
But how should i tell my application to eager-fetch a collection just this time?
In other words. I have a DAO with a get-method. EntityDao.getEntity(entityId)
This entity has a collection of Children which is lazy-loaded by default.
Lets say the dao-method is transactional.
Most of the time i only need the Entity without the children, but every once in a while i need to eager fetch the children. Now how would i go about that?
If i understand this all correct, then the session only lives within the transaction. So after the transaction ends (in this case after the dao-method) then no more lazy-fetching can be done.
So then i can either put my transaction further up my code, or eager fetch my object within the transaction?
So how would i do this eager-fetch within my transaction?
What are my options?
You shouldn’t do the transaction in the dao. The transaction should span the whole business operation, that’s the sense of having transactions.
You also destroy lazy loading. It should be transparent to the caller, which is the case when the session is still available and the children are loaded when accessing them. After closing the session, lazy loading isn’t possible anymore and entities should be accessed.