title: Showing a result from DB table as a List in Java Web App
body: <p>I'm using Spring, Hibernate and JSF for a web application.
  In my case I have a Table User having many columns, one of those is idRole. idRole is a foreign key from table Role which containes Id of the role and the role name.
  What I want to know is how to make the role name shown in my place of the ID, and make that field a List showing only possible roles (which are all the columns of the Roles table)</p>
  
  <p>Here is my ManagedBean :</p>
  
  <pre><code>package managedController;
  
  import org.primefaces.event.RowEditEvent;
  import org.springframework.dao.DataAccessException;
  import org.springframework.security.authentication.AuthenticationManager;
  import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
  import org.springframework.security.core.Authentication;
  import org.springframework.security.core.AuthenticationException;
  import org.springframework.security.core.context.SecurityContextHolder;
  import spring.model.Customer;
  import spring.service.CustomerService;
  
  import javax.faces.application.FacesMessage;
  import javax.faces.bean.ManagedBean;
  import javax.faces.bean.ManagedProperty;
  import javax.faces.bean.RequestScoped;
  import javax.faces.context.FacesContext;
  import java.io.Serializable;
  import java.util.ArrayList;
  import java.util.List;
  
  @ManagedBean(name="customerMB")
  @RequestScoped
  public class CustomerManagedBean implements Serializable {
  
      private static final long serialVersionUID = 1L;
      private static final String SUCCESS = "success";
      private static final String ERROR   = "error";
  
      @ManagedProperty(value="#{CustomerService}")
      CustomerService customerService;
  
      List&lt;Customer&gt; customerList;
  
      private int id;
      private String name;
      private String surname;
  
      public String addCustomer() {
          try {
              Customer customer = new Customer();
              customer.setId(getId());
              customer.setName(getName());
              customer.setSurname(getSurname());
              getCustomerService().addCustomer(customer);
              reset();
              return SUCCESS;
          } catch (DataAccessException e) {
              e.printStackTrace();
          }
          return ERROR;
      }
  
      public String updateCustomer(Customer customer) {
          try {
              getCustomerService().updateCustomer(customer);
              return SUCCESS;
          } catch (DataAccessException e) {
              e.printStackTrace();
          }
          return ERROR;
      }
  
      public String deleteCustomer(Customer customer) {
          try {
              getCustomerService().deleteCustomer(customer);
              customerList = null;
              getCustomerList();
              return SUCCESS;
          } catch (DataAccessException e) {
              e.printStackTrace();
          }
          return ERROR;
      }
  
      public void onEdit(RowEditEvent event) {
          FacesMessage msg = new FacesMessage("Item Edited");
          FacesContext.getCurrentInstance().addMessage(null, msg);
          updateCustomer((Customer)event.getObject());
      }
  
      public void onCancel(RowEditEvent event) {
          FacesMessage msg = new FacesMessage("Item Cancelled");
          FacesContext.getCurrentInstance().addMessage(null, msg);
      }
  
      public void reset() {
          this.setId(0);
          this.setName("");
          this.setSurname("");
      }
  
      public List&lt;Customer&gt; getCustomerList() {
          if(customerList == null){
              customerList = new ArrayList&lt;Customer&gt;();
              customerList.addAll(getCustomerService().getCustomers());
          }
          return customerList;
      }
  
      public CustomerService getCustomerService() {
          return customerService;
      }
  
      public void setCustomerService(CustomerService customerService) {
          this.customerService = customerService;
      }
  
      public void setCustomerList(List&lt;Customer&gt; customerList) {
          this.customerList = customerList;
      }
  
      public int getId() {
          return id;
      }
  
      public void setId(int id) {
          this.id = id;
      }
  
      public String getName() {
          return name;
      }
  
      public void setName(String name) {
          this.name = name;
      }
  
      public String getSurname() {
          return surname;
      }
  
      public void setSurname(String surname) {
          this.surname = surname;
      }
  
  }
  </code></pre>
  
  <p>and here is the file where I show them (Users or Customers let's say) :</p>
  
  <pre><code>&lt;html xmlns="http://www.w3.org/1999/xhtml"
        xmlns:h="http://java.sun.com/jsf/html"
        xmlns:f="http://java.sun.com/jsf/core"
        xmlns:p="http://primefaces.org/ui"
        xmlns:sec="http://www.springframework.org/security/tags"&gt;
  
  &lt;h:head&gt;
      &lt;title&gt;Welcome&lt;/title&gt;
  
  &lt;/h:head&gt;
  
  &lt;h:body&gt;
  
      &lt;!-- View element level Spring Security : only can view for who has the role 'ROLE_ADMIN' --&gt;
      &lt;sec:authorize access="hasAnyRole('ROLE_ADMIN')"&gt;
          &lt;h:form id="form2"&gt;
              &lt;table&gt;
                  &lt;tr&gt;
                      &lt;td&gt;&lt;h:outputLabel for="id" value="Id : "/&gt;&lt;/td&gt;
                      &lt;td&gt;&lt;p:inputText id="id" value="#{customerMB.id}"&gt;
                          &lt;f:converter converterId="javax.faces.Integer"/&gt;
                          &lt;p:ajax event="blur" update="idMsg"/&gt;
                      &lt;/p:inputText&gt;
                          &lt;p:message id="idMsg" for="id" display="icon"/&gt;
                      &lt;/td&gt;
                  &lt;/tr&gt;
                  &lt;tr&gt;
                      &lt;td&gt;&lt;h:outputLabel for="name" value="Name : "/&gt;&lt;/td&gt;
                      &lt;td&gt;&lt;p:inputText id="name" value="#{customerMB.name}"&gt;
                          &lt;f:validateLength minimum="1"/&gt;
                          &lt;p:ajax event="blur" update="nameMsg"/&gt;
                      &lt;/p:inputText&gt;
                          &lt;p:message id="nameMsg" for="name" display="icon"/&gt;
                      &lt;/td&gt;
                  &lt;/tr&gt;
                  &lt;tr&gt;
                      &lt;td&gt;&lt;h:outputLabel for="surname" value="Surname : "/&gt;&lt;/td&gt;
                      &lt;td&gt;&lt;p:inputText id="surname" value="#{customerMB.surname}"&gt;
                          &lt;f:validateLength minimum="1"/&gt;
                          &lt;p:ajax event="blur" update="surnameMsg"/&gt;
                      &lt;/p:inputText&gt;
                          &lt;p:message id="surnameMsg" for="surname" display="icon"/&gt;
                      &lt;/td&gt;
                  &lt;/tr&gt;
                  &lt;tr&gt;
                      &lt;td&gt;&lt;p:commandButton id="addUser" value="Add" action="#{customerMB.addCustomer}" ajax="false"/&gt;&lt;/td&gt;
                      &lt;td&gt;&lt;p:commandButton id="reset" value="Reset" action="#{customerMB.reset}" ajax="false"/&gt;&lt;/td&gt;
                  &lt;/tr&gt;
              &lt;/table&gt;
          &lt;/h:form&gt;
      &lt;/sec:authorize&gt;
  
      &lt;br/&gt;
  
      &lt;h:form id="form1"&gt;
          &lt;p:growl id="messages" showDetail="true"/&gt;
          &lt;p:dataTable id="customers" var="customer" value="#{customerMB.customerList}" style="width: 10%" editable="true"&gt;
              &lt;p:ajax event="rowEdit" listener="#{customerMB.onEdit}" update=":form1:messages"/&gt;
              &lt;p:ajax event="rowEditCancel" listener="#{customerMB.onCancel}" update=":form1:messages"/&gt;
              &lt;p:column headerText="ID"&gt;
                  &lt;h:outputText value="#{customer.id}"/&gt;
              &lt;/p:column&gt;
              &lt;p:column headerText="Name"&gt;
                  &lt;f:facet name="header"&gt;
                      &lt;h:outputText value="Name"/&gt;
                  &lt;/f:facet&gt;
                  &lt;p:cellEditor&gt;
                      &lt;f:facet name="output"&gt;
                          &lt;h:outputText value="#{customer.name}"/&gt;
                      &lt;/f:facet&gt;
                      &lt;f:facet name="input"&gt;
                          &lt;p:inputText value="#{customer.name}" label="name"/&gt;
                      &lt;/f:facet&gt;
                  &lt;/p:cellEditor&gt;
              &lt;/p:column&gt;
              &lt;p:column headerText="Surname"&gt;
                  &lt;p:cellEditor&gt;
                      &lt;f:facet name="output"&gt;
                          &lt;h:outputText value="#{customer.surname}"/&gt;
                      &lt;/f:facet&gt;
                      &lt;f:facet name="input"&gt;
                          &lt;p:inputText value="#{customer.surname}" label="surname"/&gt;
                      &lt;/f:facet&gt;
                  &lt;/p:cellEditor&gt;
              &lt;/p:column&gt;
              &lt;sec:authorize access="hasAnyRole('ROLE_ADMIN')"&gt;
                  &lt;p:column&gt;
                      &lt;f:facet name="header"&gt;Delete&lt;/f:facet&gt;
                      &lt;h:commandLink value="Delete" action="#{customerMB.deleteCustomer(customer)}"/&gt;
                  &lt;/p:column&gt;
                  &lt;p:column style="width:20%"&gt;
                      &lt;f:facet name="header"&gt;Update&lt;/f:facet&gt;
                      &lt;p:rowEditor/&gt;
                  &lt;/p:column&gt;
              &lt;/sec:authorize&gt;
          &lt;/p:dataTable&gt;
          &lt;br/&gt;
          &lt;p:commandButton value="Logout" id="logout" action="#{loginBean.logout}"/&gt;
      &lt;/h:form&gt;
  &lt;/h:body&gt;
  
  &lt;/html&gt;
  </code></pre>
  
  <p>Thank you</p>