<p>The situation is this:</p>

<ul>
<li>You have a Hibernate context with an
object graph that has some lazy
loading defined.  </li>
<li>You want to use
the Hibernate objects in your UI as
is without having to copy the data
somewhere.  </li>
<li>There are different UI
contexts that require different
amounts of data.  </li>
<li>The data is too
big to just eager load the whole
graph each time.</li>
</ul>

<p>What is the best means to load all the appropriate objects in the object graph in a configurable way so that they can be accessed without having to go back to the database to load more data?</p>

<p>Any help.</p>

[{'answer_id': 79933, 'author': 'sirrocco', 'author_id': 5246, 'author_profile': 'https://Stackoverflow.com/users/5246', 'pm_score': 3, 'selected': True, 'text': "<p>Let's say you have the Client and at one point you have to something with his Orders and maybe he has a Bonus for his Orders. </p>\n\n<p>Then I would define a Repository with a fluent interface that will allow me to say something like :</p>\n\n<pre><code>new ClientRepo().LoadClientBy(id)\n                .WithOrders()\n                .WithBonus()\n                .OrderByName();\n</code></pre>\n\n<p>And there you have the client with everything you need. It's preferably that you know in advance what you will need for the current operation. This way you can avoid unwanted trips to the database.(new devs in your team will usually do this - call a property and not be aware of the fact that it's actually a call to the DB)</p>\n"}, {'answer_id': 80154, 'author': 'Dónal', 'author_id': 2648, 'author_profile': 'https://Stackoverflow.com/users/2648', 'pm_score': 1, 'selected': False, 'text': "<p>If it's a webapp and you're using Spring, then OpenSessionInViewFilter could be the solution to your problems.</p>\n"}, {'answer_id': 87117, 'author': 'Roland Schneider', 'author_id': 16515, 'author_profile': 'https://Stackoverflow.com/users/16515', 'pm_score': 1, 'selected': False, 'text': '<p>An approach we use in our projects is to create a service for each view you have. Then the view fetches the sub-graph you need for this specific view, always trying to reduce the number of sqls send to the database. Therefore we are using a lot of joins to get the n:1 associated objects.</p>\n\n<p>If you are using a 2-tier desktop app directly connected to the DB you can just leave the objects attached and load additional data anytime automatically. Otherwise you have to reattach it to the session and initialize the association you need with <code>Hibernate.initialize(Object entity, String propertyName)</code></p>\n\n<p>(Out of memory, maybe not 100% correct)</p>\n'}]
['https://Stackoverflow.com/questions/79843', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/14893/']