title: JPA working with detached Entity
body: <p>I am working with JPA to get deep insight and I am referring Pro JPA2 Mastering the Java Persistance API.</p>
  
  <p>In Chapter 6 I am working with example <strong><em>Working with detached Entities</em></strong>,
  I created the example as per given guideline.
  This topic says <strong>@ManyToOne/@OneToMany bidirection</strong> <strong>mapping is lazy loaded</strong>.However When I created same example this is not true that relationship is lazy loaded. Relationship are eagerly loded.
  Here is my example</p>
  
  <p><strong>My JSF Templete:</strong></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8" ?&gt;
  &lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
  &lt;html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:f="http://java.sun.com/jsf/core"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:p="http://primefaces.org/ui"&gt;
  &lt;h:head&gt;
      &lt;meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /&gt;
      &lt;title&gt;Student Registration&lt;/title&gt;
  &lt;/h:head&gt;
  &lt;h:body&gt;
      &lt;h2&gt;Enter student Details&lt;/h2&gt;
      &lt;h:form&gt;
          &lt;h:commandButton actionListener="#{employeeService.saveEmployee}"
              value="Save" /&gt;
          &lt;h:inputText value="#{employeeService.empId}" id="empId" /&gt;
          &lt;h:commandButton actionListener="#{employeeService.updateEmployee}"
              value="update" /&gt;
      &lt;/h:form&gt;
      &lt;form method="post" action="employee"&gt;
          &lt;input type="submit" value="go" /&gt;
      &lt;/form&gt;
  &lt;/h:body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p><strong>EmployeeServlet</strong></p>
  
  <pre><code>package com.jsfapp.servlet;
  
  import java.io.IOException;
  import java.util.List;
  
  import javax.ejb.EJB;
  import javax.servlet.ServletException;
  import javax.servlet.annotation.WebServlet;
  import javax.servlet.http.HttpServlet;
  import javax.servlet.http.HttpServletRequest;
  import javax.servlet.http.HttpServletResponse;
  
  import com.jsfapp.dao.EmployeeDAO;
  import com.jsfapp.jpa.entities.Department;
  
  @WebServlet(urlPatterns = "/employee")
  public class EmployeeServlet extends HttpServlet {
  
      @EJB
      private EmployeeDAO employeeService;
  
      /**
       * 
       */
      private static final long serialVersionUID = 1L;
  
      @Override
      protected void doPost(HttpServletRequest req, HttpServletResponse resp)
              throws ServletException, IOException {
  
          /*
           * List&lt;Employee&gt; employees = employeeService.findAll();
           * req.setAttribute("employees", employees);
           * System.out.println("===&gt;"+employees.get(0).getName());;
           */
          List&lt;Department&gt; dept = employeeService.getDepartment();
          req.setAttribute("departments", dept);
          this.getServletContext().getRequestDispatcher("/emp.jsp")
                  .forward(req, resp);
  
      }
  
  }
  </code></pre>
  
  <p><strong>EmployeeDAO</strong></p>
  
  <pre><code>package com.jsfapp.dao;
  
  import java.util.List;
  
  import javax.ejb.LocalBean;
  import javax.ejb.Stateless;
  import javax.persistence.EntityManager;
  import javax.persistence.PersistenceContext;
  import javax.persistence.TypedQuery;
  
  import com.jsfapp.jpa.entities.Department;
  import com.jsfapp.jpa.entities.Employee;
  
  @Stateless(name = "employeeDAO")
  @LocalBean
  public class EmployeeDAO {
  
      @PersistenceContext(unitName = "JSFApplication")
      private EntityManager em;
  
      public void saveEmployee(Employee emp) {
          em.persist(emp);
      }
  
      public void updateEmployee(Employee emp) {
          Employee employeeMerged = em.merge(emp);
          employeeMerged.setName("AfterMerge");
      }
  
      public Employee findById(Integer empId) {
          return em.find(Employee.class, empId);
      }
  
      public List&lt;Employee&gt; findAll() {
          TypedQuery&lt;Employee&gt; employeeQuery = em.createQuery(
                  "SELECT e FROM Employee e", Employee.class);
          return employeeQuery.getResultList();
      }
  
      public List&lt;Department&gt; getDepartment() {
          TypedQuery&lt;Department&gt; dept = em.createQuery(
                  "SELECT d FROM Department d",Department.class);
          //dept.setParameter("deptId", 3);
          List&lt;Department&gt; depts = dept.getResultList();
          return depts;
      }
  }
  </code></pre>
  
  <p><strong>Department Entity</strong></p>
  
  <pre><code>package com.jsfapp.jpa.entities;
  
  import java.util.List;
  
  import javax.persistence.Entity;
  import javax.persistence.GeneratedValue;
  import javax.persistence.GenerationType;
  import javax.persistence.Id;
  import javax.persistence.OneToMany;
  import javax.persistence.Table;
  
  @Entity
  @Table
  public class Department {
  
      @Id
      @GeneratedValue(strategy = GenerationType.AUTO)
      private Integer id;
      private String name;
      @OneToMany(mappedBy = "department")
      private List&lt;Employee&gt; employees;
  
      public Integer getId() {
          return id;
      }
  
      public void setId(Integer id) {
          this.id = id;
      }
  
      public String getName() {
          return name;
      }
  
      public void setName(String name) {
          this.name = name;
      }
  
      public List&lt;Employee&gt; getEmployees() {
          return employees;
      }
  
      public void setEmployees(List&lt;Employee&gt; employees) {
          this.employees = employees;
      }
  }
  </code></pre>
  
  <p><strong>Employee Entity</strong></p>
  
  <pre><code>  package com.jsfapp.jpa.entities;
  
  import javax.persistence.CascadeType;
  import javax.persistence.Entity;
  import javax.persistence.GeneratedValue;
  import javax.persistence.GenerationType;
  import javax.persistence.Id;
  import javax.persistence.JoinColumn;
  import javax.persistence.ManyToOne;
  import javax.persistence.Table;
  
  @Entity
  @Table(name = "Employee")
  public class Employee {
  
      @Id
      @GeneratedValue(strategy = GenerationType.AUTO)
      private Integer id;
      private String name;
      @ManyToOne(cascade = { CascadeType.PERSIST, CascadeType.MERGE })
      @JoinColumn(name = "DEPT_ID")
      private Department department;
      @ManyToOne(cascade = { CascadeType.PERSIST, CascadeType.MERGE })
      @JoinColumn(name = "EMP_ADDR_ID")
      private Address address;
  
      public Integer getId() {
          return id;
      }
  
      public void setId(Integer id) {
          this.id = id;
      }
  
      public String getName() {
          return name;
      }
  
      public void setName(String name) {
          this.name = name;
      }
  
      public Address getAddress() {
          return address;
      }
  
      public void setAddress(Address address) {
          this.address = address;
      }
  
      public Department getDepartment() {
          return department;
      }
  
      public void setDepartment(Department department) {
          this.department = department;
      }
  
  }
  </code></pre>
  
  <p>now when I get Department Entity It load  lazy loaded Employee . I know that it is provider dependent but why it is loaded eagerly.
  please help</p>
  
  <p><strong>P.S. I am using glassfish3 ,Eclipse Link 2.3 for JPA</strong></p>