title: LazyInitializationException - could not initialize proxy - no Session
body: <p>I have a problem with LazyInitializationException and I don't know how to fix it.</p>
  
  <pre><code>for (Long id : employeeIds)
      {
          List&lt;ProjectEmployee&gt; projectEmployeeList = projectEmployeeService.findProjectEmployeesWithinDates(id,
                  startDate, endDate);
  
          // if no data, then continue with next employee
          if (projectEmployeeList.isEmpty())
          {
              continue;
          }
  
          gridCreated = true;
  
          Employee employee = projectEmployeeList.get(0).getEmployee();
          Label titleLabel = new Label(employee.getPerson().getSurname() + " " + employee.getPerson().getName() + " ["
                                       + employee.getRole().getHumanizedRole() + "]");
          titleLabel.setStyleName("header-bold");
  
          ProjectEmployeePanel projectEmployeePanel = new ProjectEmployeePanel(id, startDate, endDate);
          gridPanelsLayout.addComponents(titleLabel, projectEmployeePanel);
      }
  </code></pre>
  
  <p>Before the problem was when I was calling .getperson=null but I fix the call findProjectEmployeesWithinDates asking there to get the person. But then I got the exception when I call the 'findProjectEmployeesWithinDates'.
  The code findProjectEmployeesWithinDates:</p>
  
  <pre><code>    public List&lt;ProjectEmployee&gt; findProjectEmployeesWithinDates(Long employeeId, LocalDate startDate, LocalDate endDate) {
      List&lt;Long&gt; list = new ArrayList&lt;&gt;();
      list.add(employeeId);
      List&lt;ProjectEmployee&gt; listProjectEmployees = projectEmployeeRepository.findProjectEmployeesWithinDates(list,
              LocaleUtils.getDateFromLocalDate(startDate, LocaleUtils.APPLICATION_DEFAULT_ZONE_ID),
              LocaleUtils.getDateFromLocalDate(endDate, LocaleUtils.APPLICATION_DEFAULT_ZONE_ID));
      for (ProjectEmployee pe : listProjectEmployees)
      {
          Hibernate.initialize(pe.getEmployee());
          Hibernate.initialize(pe.getEmployee().getPerson());
  
      }
      return listProjectEmployees;
  }
  </code></pre>
  
  <p>So using debbug i saw that :</p>
  
  <pre><code> Hibernate.initialize(pe.getEmployee()); ----line 105
  Hibernate.initialize(pe.getEmployee().getPerson()); ---line 106
  </code></pre>
  
  <p>it goes at the first line here at the for loop in the findProjectEmployeesWithinDates but not at the second, and this is where the exception happens.</p>
  
  <p>the error I get </p>
  
  <pre><code>Caused by: org.hibernate.LazyInitializationException: could not initialize proxy - no Session
  </code></pre>
  
  <p>at org.hibernate.proxy.AbstractLazyInitializer.initialize(AbstractLazyInitializer.java:165) ~[hibernate-core-4.3.6.Final.jar:4.3.6.Final]
  at org.hibernate.Hibernate.initialize(Hibernate.java:75) ~[hibernate-core-4.3.6.Final.jar:4.3.6.Final]
  at com.xitee.ccpt.service.ProjectEmployeeService.findProjectEmployeesWithinDates(ProjectEmployeeService.java:105) ~[classes/:na]
  at com.xitee.ccpt.service.ProjectEmployeeService$$FastClassBySpringCGLIB$$63bfc6f9.invoke() ~[spring-core-4.1.1.RELEASE.jar:na]</p>
  
  <p>Project Employee class:</p>
  
  <pre><code>@Entity
  </code></pre>
  
  <p>@Table(name = "employee", schema = "ccpt_data")
  @NamedQuery(name = "Employee.findAll", query = "SELECT e FROM Employee e")
  public class Employee implements Serializable
  {
      private static final long serialVersionUID = 1L;</p>
  
  <pre><code>@Id
  @Column(name = "employee_id")
  @SequenceGenerator(name = "EMPLOYEE_ID_GENERATOR", sequenceName = "employee_id_seq", allocationSize = 1)
  @GeneratedValue(strategy = GenerationType.AUTO, generator = "EMPLOYEE_ID_GENERATOR")
  private Long employeeId;
  
  @OneToOne(fetch = FetchType.LAZY)
  @JoinColumn(name = "person_id")
  private Person person;
  
  @Column(name = "monthly_cost")
  private String monthlyCost;
  
  @Enumerated(EnumType.STRING)
  @Column(name = "role")
  private EmployeeRole role;
  
  @Column(name = "employee_manager")
  private String employeeManager;
  
  @Column(name = "obsolete")
  private Boolean obsolete;
  
  @Column(name = "bank_account_number")
  private String bankAccountNumber;
  
  @Column(name = "last_employer")
  private String lastEmployer;
  
  @Column(name = "starting_day")
  private String startingDay;
  
  @Column(name = "hours")
  private Short hours;
  
  @OneToMany(mappedBy = "employee", cascade =
      {
       CascadeType.ALL
      }, orphanRemoval = true)
  private Set&lt;EmployeeWorkload&gt; employeeWorkloads;
  
  @OneToMany(mappedBy = "employee", fetch = FetchType.LAZY, orphanRemoval = true)
  private Set&lt;ProjectEmployee&gt; projectEmployee;
  
  @OneToMany(mappedBy = "employee", cascade =
      {
       CascadeType.PERSIST
      }, orphanRemoval = true)
  private Set&lt;Qualification&gt; qualifications;
  
  @OneToMany(mappedBy = "employee", cascade =
      {
       CascadeType.PERSIST
      }, orphanRemoval = true)
  private List&lt;CareerExperience&gt; careerExperiences;
  
  @Transient
  private Map&lt;Integer, String&gt; exportOptions;
  
  @OneToMany(mappedBy = "employee", fetch = FetchType.LAZY, orphanRemoval = true)
  private Set&lt;ProjectEmployeeRejection&gt; projectEmployeeRejections;
  
  @Transient
  private boolean decrypted = true; // allows editing and viewing for users with no encryption rights
  
  /**
   * Initialization vector used for encryption of this employee or NO_KEY if no encryption was used
   *
   * @since 0.4.0
   */
  @Column(name = "iv")
  private String iv;
  
  @Column(name = "cis_employee_id")
  private Long cISEmployeeId;
  
  @Column(name = "experience_summary")
  private String experienceSummary;
  
  @Enumerated(EnumType.STRING)
  @Column(name = "employee_job_type")
  private EmployeeJobType employeeJobType;
  
  @Column(name = "ending_day")
  @Type(type = "date")
  private Date endingDay;
  
  @Column(name = "main_skill")
  private String mainSkill;
  
  public Employee()
  {
  }
  
  public Long getEmployeeId()
  {
      return employeeId;
  }
  
  public void setEmployeeId(Long employeeId)
  {
      this.employeeId = employeeId;
  }
  
  public Person getPerson()
  {
      return person;
  }
  
  public void setPerson(Person person)
  {
      this.person = person;
  }
  
  public String getExperienceSummary()
  {
      return experienceSummary;
  }
  
  public void setExperienceSummary(String experienceSummary)
  {
      this.experienceSummary = experienceSummary;
  }
  </code></pre>
  
  <p>Can anyone help me with this problem, please?</p>