Announcement Announcement Module
No announcement yet.
Initialize lazy data Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • Initialize lazy data


    How do I initialize lazy loaded data correctly? I have a list of employees (employeeList), and I want to be sure that all data is already in memory, when I pass the list (employeeList) to the JSP page.

    Is the correct way to code the Spring web Controller in the following way:

    List<Employee> employeeList = someService.findAllEmployees();
    The problem with the above is that there is now a binding to Hibernate on the web layer, which is not a good thing. On the other hand, I do not want the service to always load fully initialized list of employees...

  • #2
    There are a few ways to do this, if you want to hide the Hibernate dependency though I'd suggest doing this in the DAO.

    #1. Mark the assocation lazy=false
    #2. Use HibernateTemplate.initialize
    #3. If you using HQL or QueryByCriteria tell it to eager fetch
    #4. Use the collection within a transaction which forces loading e.g. myCollection.isEmpty(), myCollection.size()