Organizational Research By

Surprising Reserch Topic

How to Properly use Lazy Loading w/ Spring in hibernet ?

How to Properly use Lazy Loading w/ Spring

So having outlined the differences between these two approaches, I hope you realize that Lazy loading is a suggested route to take your application code in most scenarios. So having said that, how do we make use of Lazy loading with a Spring enabled application?

The important thing to remember is that when using a Spring/Hibernate application, there’s a bit of code that is handled for you in the background that you don’t actually see. The code that you don’t see is the transactional code (enabled by the @Transactional annotation in your DAOs) and this is something that you always need to keep in mind when using Lazy loading.

Having said that, let’s take a look at an example of Lazy loading using our User object. Let’s assume we have our User object which has a Lazy load fetch enabled to the Profile object. So we will need to design our UserDao with this relationship in mind. One approach is to create two separate getter methods like so:


  1. /** 
  2. * This UserDao object is being constructed under the assumption that the relationship 
  3. *  to the Profile object is being Lazily loaded using FetchType.LAZY 
  4. */  
  5. @Repository  
  6. @Transactional  
  7. public class UserDao  
  8. {  
  9.   @Autowired  
  10.   private SessionFactory sessionFactory;  
  12.   public User getUserById(Long userId)  
  13.   {  
  14.     Session session = sessionFactory.getCurrentSession();  
  16.     // this will invoke the   
  17.     return (User) session.createCriteria(User.class).add(Restrictions.idEq(userId)).uniqueResult();  
  18.   }  
  20.   public User getUserWithProfileById(Long userId)  
  21.   {  
  22.     Session session = sessionFactory.getCurrentSession();  
  24.     User user = (Users) session.createCriteria(User.class).add(Restrictions.idEq(userId)).uniqueResult();  
  25.     // this will force SQL to execute the query that will join with the user's profile and populate  
  26.     //  the appropriate information into the user object.  
  27.     Hibernate.initialize(user.getUserProfile());  
  29.     return user;  
  30.   }  
  31. }  

In the code above you see that we have created two getter methods to load the User object from our database via Hibernate.

This means that when you want to load a¬†User¬†object without any relationships (i.e. when you‚Äôre checking to see if the login information is valid) then you can call¬†getUserById¬†and this will bypass the unnecessary join to the¬†Profile¬†object/table. But if you are in a situation where you DO need to refer to the¬†User‚Äės profile, then you can call the¬†getUserWithProfileById.

asked Jul 1, 2015 in hibernate by rajesh
0 votes

Related Hot Questions

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
Anti-spam verification:
To avoid this verification in future, please log in or register.