Correct me if this is an exact duplicate, I know this topic is discussed often but can't find a definitive answer.
The question:
What is the best practical solution to handling Hibernate objects in a MVC webapp?
The details:
I am using Hibernate and want to leverage lazy loading where possible.
I am working in a MVC style webapp.
I hate getting lazy load initialization exceptions.
I hate having to reattach Hibernate objects between transactions.
The options:
hibernateObject.getRelatedObjects()
but need to say something like springService.getRelatedObjects(hibernateObject)
Am I missing something?
Have I over-thought things?
Have I under-thought things?
PS:
For a web framework I'm using ZK but don't really want a ZK specific answer.
I'm also using Spring and am cool with a Spring specific answer as it's so ubiquitous.
You can do pagination and load your data based on demand. Here I will explain step by step from scrach, an application to implement lazy loading using ajax call. I have attached the source code for this demo. Select MVC option from the next screen and click on ok button which will create a new project for you.
Lazy loading is essential when the cost of object creation is very high and the use of the object is vey rare. So, this is the scenario where it's worth implementing lazy loading. The fundamental idea of lazy loading is to load object/data when needed.
Entity Framework supports three ways to load related data - eager loading, lazy loading and explicit loading.
Yes, lazy loading is enabled in the Entity Framework ORM too, it is on by default in Entity Framework, so if you want to enable lazy loading in Entity Framework, you don't need to do anything.
Use 4-ish - Don't use open session in view, don't have your hibernate entities bubble all the way up to the view instead have transformers translate between the hibernate entities and your domain objects or 'view beans' depending on how you want to work it.
I think of Hibernate entities as just a persistence strategy not a domain model or UI representation.
There are three ways:
Use eager fetch load for your attributes: Can be a problem if you have a big datatable.
Use a filter called OpenSessionInView: this filter will keep the session open until the full load of your web page. If any hibernate object was request in this load, the session will be opened and will avoid lazy loading exception.
User VOs (Valueble Objects): In your application, will have 2 kinds of objects. A object to pass between the persistence and business layer, and a object for you view layer. For example, UserVO and UserModel. A vo will be used to transport information between view and business layer. In your business implementation, you will use the vo to fill the model object to send it to you persistence layer. Using this pattern, you wont have more lazy load exception because all needed information will be filled in your vo object when is necessary.
Some references:
OpenSessionInView
Eager Fetching Load
Hibernate Performance tips
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