Search code examples
javaarchitecturepersistencelazy-loading

Lazy loading at the business level


In my Java application I'm trying to separate business model and persistence model as strict as it's possible. So i have a set of entities on the persistence level powered by Hibernate + Spring Data, with relations, cascades, lazy loading and other features. But then I have a business model, as a different set of entities, which are being mapped in some ways on the persistent ones.

As soon as Hibernate doesn't know about this level, no lazy loading there. How do I make it? Is there a solutioin or common/better practise to implement a lazy loading without storing external keys (and other persistence stuff) at the business level?

My goal is to make a link between business and persistence as soft as possible. I understand, that as soon as I turn to entity relations and some loading from database itiniated from the business level, these levels will become bound to each other. But is there ways to minimize the dependency?

P.S. I do really need lazy loading itself, in any possible way.


Solution

  • This is best accomplished by splitting your domain into two separate Bounded Contexts. This means that you won't be able to directly reference the lazy-loaded context in the non-lazy loaded domain. You will have to drop back out to the service layer to refer to it. This is probably the pattern you are looking for anyway, if the lazily loaded information is so expensive to retrieve.

    This will drive the design of your application at a fairly high level, so be prepared to refactor things to make this work.