title: list to view with foreign key in JSP from DB
body: <p>I have problem with display datasource from DB. If I want display ID (foreign key) everything working good, but I want diaplay name.</p>
  
  <p>Working good and displays department ID:</p>
  
  <pre><code>&lt;c:if test="${!empty contactList}"&gt;
      &lt;table class="data"&gt;
          &lt;tr&gt;
              &lt;th&gt;First name&lt;/th&gt;
              &lt;th&gt;Last name&lt;/th&gt;
              &lt;th&gt;Email&lt;/th&gt;
              &lt;th&gt;Telephone&lt;/th&gt;
              &lt;th&gt;Department&lt;/th&gt;
              &lt;th&gt;&amp;nbsp;&lt;/th&gt;
          &lt;/tr&gt;
          &lt;c:forEach items="${contactList}" var="contact"&gt;
              &lt;tr&gt;
                  &lt;td&gt;${contact.firstname}&lt;/td&gt;
                  &lt;td&gt;${contact.lastname}&lt;/td&gt;
                  &lt;td&gt;${contact.email}&lt;/td&gt;
                  &lt;td&gt;${contact.telephone}&lt;/td&gt;
                  &lt;td&gt;${contact.department.id}&lt;/td&gt;
  
  
              &lt;!--    &lt;td&gt;&lt;a href="delete/${contact.id}.html"&gt;delete&lt;/a&gt;&lt;/td&gt;--&gt;
              &lt;td&gt;
                  &amp;nbsp;&lt;a href="updateContact.do?id=${contact.id}"&gt;Edit&lt;/a&gt;
                  &amp;nbsp;&amp;nbsp;&lt;a href="javascript:deleteContact('deleteContact.do?id=${contact.id}');"&gt;delete&lt;/a&gt;
  
              &lt;/td&gt;
  
              &lt;/tr&gt;
  </code></pre>
  
  <p>I want:</p>
  
  <pre><code>&lt;td&gt;${contact.department.name}&lt;/td&gt;
  </code></pre>
  
  <p>controller:</p>
  
  <pre><code> @RequestMapping("/showContacts")
      public String listContacts(Map&lt;String, Object&gt; map) {
  
          User user = (User)SecurityContextHolder.getContext().getAuthentication().getPrincipal();
          String name = user.getUsername();       
          map.put("username", name);
          map.put("contact", new Contact());      
      //  map.put("department", new Department());
          map.put("contactList", contactService.listContact());
  
          return "showContacts";
      }
  </code></pre>
  
  <p>Entity with foreign key:</p>
  
  <pre><code>package pl.ivmx.contact.form;
  
  
  
  import javax.persistence.Column;
  import javax.persistence.Entity;
  import javax.persistence.FetchType;
  import javax.persistence.GeneratedValue;
  import javax.persistence.GenerationType;
  import javax.persistence.Id;
  import javax.persistence.JoinColumn;
  import javax.persistence.ManyToOne;
  import javax.persistence.Table;
  
  import pl.ivmx.department.form.Department;
  
  @Entity
  @Table(name="CONTACTS")
  public class Contact {
  
      @Id
      @Column(name="ID")
      @GeneratedValue(strategy = GenerationType.AUTO)
      private Integer id;
  
      @Column(name="FIRSTNAME")
      private String firstname;
  
      @Column(name="LASTNAME")
      private String lastname;
  
      @Column(name="EMAIL")
      private String email;
  
      @Column(name="TELEPHONE")
      private int telephone;
  
  
      @ManyToOne(fetch = FetchType.LAZY)
      @JoinColumn(name = "Department_ID", nullable = false)
      private Department department;
  
  
      public String getEmail() {
          return email;
      }
      public int getTelephone() {
          return telephone;
      }
      public void setEmail(String email) {
          this.email = email;
      }
      public void setTelephone(int telephone) {
          this.telephone = telephone;
      }
      public String getFirstname() {
          return firstname;
      }
      public String getLastname() {
          return lastname;
      }
      public void setFirstname(String firstname) {
          this.firstname = firstname;
      }
      public void setLastname(String lastname) {
          this.lastname = lastname;
      }
      public Integer getId() {
          return id;
      }
      public void setId(Integer id) {
          this.id = id;
      }
      public Department getDepartment() {
          return department;
      }
      public void setDepartment(Department department) {
          this.department = department;
      }
  
  }
  </code></pre>
  
  <p>Second entity:</p>
  
  <pre><code>package pl.ivmx.department.form;
  
  import java.io.Serializable;
  import java.util.HashSet;
  import java.util.Set;
  
  import javax.persistence.CascadeType;
  import javax.persistence.Column;
  import javax.persistence.Entity;
  import javax.persistence.FetchType;
  import javax.persistence.GeneratedValue;
  import javax.persistence.GenerationType;
  import javax.persistence.Id;
  import javax.persistence.Table;
  import javax.persistence.OneToMany;
  
  import pl.ivmx.contact.form.Contact;;
  
  @Entity
  @Table(name = "department")
  public class Department implements Serializable{
  
      private int id;
      private String name;
      private Set&lt;Contact&gt; contact = new HashSet&lt;Contact&gt;();
  
  
      @Id
      @GeneratedValue(strategy = GenerationType.AUTO)
      @Column(name = "Department_ID")
      public int getId() {
          return id;
      }
  
      public void setId(int id) {
          this.id = id;
      }
  
      @Column(name = "Name")
      public String getName() {
          return name;
      }
      public void setName(String name) {
          this.name = name;
      }
  
      @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "department")
      public Set&lt;Contact&gt; getContact() {
          return contact;
      }
      public void setContact(Set&lt;Contact&gt; contact) {
          this.contact = contact;
      }
  
  
  }
  </code></pre>
  
  <p>ERROR:</p>
  
  <p>FeachType.EAGER:</p>
  
  <pre><code>type Exception report
  
  message javax.el.ELException: Error reading 'name' on type pl.ivmx.department.form.Department_$$_javassist_0
  
  description The server encountered an internal error (javax.el.ELException: Error reading 'name' on type pl.ivmx.department.form.Department_$$_javassist_0) that prevented it from fulfilling this request.
  
  exception
  
  org.apache.jasper.JasperException: javax.el.ELException: Error reading 'name' on type pl.ivmx.department.form.Department_$$_javassist_0
      org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:549)
      org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:470)
      org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
      org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
      javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
      org.springframework.web.servlet.view.InternalResourceView.renderMergedOutputModel(InternalResourceView.java:238)
      org.springframework.web.servlet.view.AbstractView.render(AbstractView.java:262)
      org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1180)
      org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:950)
      org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:852)
      org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:882)
      org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:778)
      javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
      javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
      org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:369)
      org.springframework.security.web.access.intercept.FilterSecurityInterceptor.invoke(FilterSecurityInterceptor.java:109)
      org.springframework.security.web.access.intercept.FilterSecurityInterceptor.doFilter(FilterSecurityInterceptor.java:83)
      org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:381)
      org.springframework.security.web.access.ExceptionTranslationFilter.doFilter(ExceptionTranslationFilter.java:97)
      org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:381)
      org.springframework.security.web.session.SessionManagementFilter.doFilter(SessionManagementFilter.java:100)
      org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:381)
      org.springframework.security.web.authentication.AnonymousAuthenticationFilter.doFilter(AnonymousAuthenticationFilter.java:78)
      org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:381)
      org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter.doFilter(SecurityContextHolderAwareRequestFilter.java:54)
      org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:381)
      org.springframework.security.web.savedrequest.RequestCacheAwareFilter.doFilter(RequestCacheAwareFilter.java:35)
      org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:381)
      org.springframework.security.web.authentication.www.BasicAuthenticationFilter.doFilter(BasicAuthenticationFilter.java:177)
      org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:381)
      org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter.doFilter(AbstractAuthenticationProcessingFilter.java:187)
      org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:381)
      org.springframework.security.web.authentication.logout.LogoutFilter.doFilter(LogoutFilter.java:105)
      org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:381)
      org.springframework.security.web.context.SecurityContextPersistenceFilter.doFilter(SecurityContextPersistenceFilter.java:79)
      org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:381)
      org.springframework.security.web.FilterChainProxy.doFilter(FilterChainProxy.java:168)
      org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:346)
      org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:259)
  
  root cause
  
  javax.el.ELException: Error reading 'name' on type pl.ivmx.department.form.Department_$$_javassist_0
      javax.el.BeanELResolver.getValue(BeanELResolver.java:98)
      javax.el.CompositeELResolver.getValue(CompositeELResolver.java:67)
      org.apache.el.parser.AstValue.getValue(AstValue.java:169)
      org.apache.el.ValueExpressionImpl.getValue(ValueExpressionImpl.java:189)
      org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate(PageContextImpl.java:985)
      org.apache.jsp.WEB_002dINF.pages.showContacts_jsp._jspx_meth_c_005fforEach_005f0(showContacts_jsp.java:368)
      org.apache.jsp.WEB_002dINF.pages.showContacts_jsp._jspx_meth_c_005fif_005f2(showContacts_jsp.java:318)
      org.apache.jsp.WEB_002dINF.pages.showContacts_jsp._jspService(showContacts_jsp.java:137)
      org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
      javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
      org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:432)
      org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
      org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
      javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
      org.springframework.web.servlet.view.InternalResourceView.renderMergedOutputModel(InternalResourceView.java:238)
      org.springframework.web.servlet.view.AbstractView.render(AbstractView.java:262)
      org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1180)
      org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:950)
      org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:852)
      org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:882)
      org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:778)
      javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
      javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
      org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:369)
      org.springframework.security.web.access.intercept.FilterSecurityInterceptor.invoke(FilterSecurityInterceptor.java:109)
      org.springframework.security.web.access.intercept.FilterSecurityInterceptor.doFilter(FilterSecurityInterceptor.java:83)
      org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:381)
      org.springframework.security.web.access.ExceptionTranslationFilter.doFilter(ExceptionTranslationFilter.java:97)
      org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:381)
      org.springframework.security.web.session.SessionManagementFilter.doFilter(SessionManagementFilter.java:100)
      org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:381)
      org.springframework.security.web.authentication.AnonymousAuthenticationFilter.doFilter(AnonymousAuthenticationFilter.java:78)
      org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:381)
      org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter.doFilter(SecurityContextHolderAwareRequestFilter.java:54)
      org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:381)
      org.springframework.security.web.savedrequest.RequestCacheAwareFilter.doFilter(RequestCacheAwareFilter.java:35)
      org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:381)
      org.springframework.security.web.authentication.www.BasicAuthenticationFilter.doFilter(BasicAuthenticationFilter.java:177)
      org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:381)
      org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter.doFilter(AbstractAuthenticationProcessingFilter.java:187)
      org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:381)
      org.springframework.security.web.authentication.logout.LogoutFilter.doFilter(LogoutFilter.java:105)
      org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:381)
      org.springframework.security.web.context.SecurityContextPersistenceFilter.doFilter(SecurityContextPersistenceFilter.java:79)
      org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:381)
      org.springframework.security.web.FilterChainProxy.doFilter(FilterChainProxy.java:168)
      org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:346)
      org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:259)
  
  root cause
  
  org.hibernate.LazyInitializationException: could not initialize proxy - no Session
      org.hibernate.proxy.AbstractLazyInitializer.initialize(AbstractLazyInitializer.java:132)
      org.hibernate.proxy.AbstractLazyInitializer.getImplementation(AbstractLazyInitializer.java:174)
      org.hibernate.proxy.pojo.javassist.JavassistLazyInitializer.invoke(JavassistLazyInitializer.java:190)
      pl.ivmx.department.form.Department_$$_javassist_0.getName(Department_$$_javassist_0.java)
      sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
      sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
      java.lang.reflect.Method.invoke(Method.java:601)
      javax.el.BeanELResolver.getValue(BeanELResolver.java:87)
      javax.el.CompositeELResolver.getValue(CompositeELResolver.java:67)
      org.apache.el.parser.AstValue.getValue(AstValue.java:169)
      org.apache.el.ValueExpressionImpl.getValue(ValueExpressionImpl.java:189)
      org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate(PageContextImpl.java:985)
      org.apache.jsp.WEB_002dINF.pages.showContacts_jsp._jspx_meth_c_005fforEach_005f0(showContacts_jsp.java:368)
      org.apache.jsp.WEB_002dINF.pages.showContacts_jsp._jspx_meth_c_005fif_005f2(showContacts_jsp.java:318)
      org.apache.jsp.WEB_002dINF.pages.showContacts_jsp._jspService(showContacts_jsp.java:137)
      org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
      javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
      org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:432)
      org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
      org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
      javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
      org.springframework.web.servlet.view.InternalResourceView.renderMergedOutputModel(InternalResourceView.java:238)
      org.springframework.web.servlet.view.AbstractView.render(AbstractView.java:262)
      org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1180)
      org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:950)
      org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:852)
      org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:882)
      org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:778)
      javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
      javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
      org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:369)
      org.springframework.security.web.access.intercept.FilterSecurityInterceptor.invoke(FilterSecurityInterceptor.java:109)
      org.springframework.security.web.access.intercept.FilterSecurityInterceptor.doFilter(FilterSecurityInterceptor.java:83)
      org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:381)
      org.springframework.security.web.access.ExceptionTranslationFilter.doFilter(ExceptionTranslationFilter.java:97)
      org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:381)
      org.springframework.security.web.session.SessionManagementFilter.doFilter(SessionManagementFilter.java:100)
      org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:381)
      org.springframework.security.web.authentication.AnonymousAuthenticationFilter.doFilter(AnonymousAuthenticationFilter.java:78)
      org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:381)
      org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter.doFilter(SecurityContextHolderAwareRequestFilter.java:54)
      org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:381)
      org.springframework.security.web.savedrequest.RequestCacheAwareFilter.doFilter(RequestCacheAwareFilter.java:35)
      org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:381)
      org.springframework.security.web.authentication.www.BasicAuthenticationFilter.doFilter(BasicAuthenticationFilter.java:177)
      org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:381)
      org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter.doFilter(AbstractAuthenticationProcessingFilter.java:187)
      org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:381)
      org.springframework.security.web.authentication.logout.LogoutFilter.doFilter(LogoutFilter.java:105)
      org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:381)
      org.springframework.security.web.context.SecurityContextPersistenceFilter.doFilter(SecurityContextPersistenceFilter.java:79)
      org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:381)
      org.springframework.security.web.FilterChainProxy.doFilter(FilterChainProxy.java:168)
      org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:346)
      org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:259)
  </code></pre>