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

  • Initialize lazy data

    Hello,

    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:

    Code:
    List<Employee> employeeList = someService.findAllEmployees();
    Hibernate.initialize(employeeList);
    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
    http://www.springframework.org/docs/...va.lang.Object)
    #3. If you using HQL or QueryByCriteria tell it to eager fetch
    http://www.hibernate.org/hib_docs/v3...queryhql-joins
    http://www.hibernate.org/hib_docs/v3...-fetching-lazy
    #4. Use the collection within a transaction which forces loading e.g. myCollection.isEmpty(), myCollection.size()

    Comment

    Working...
    X