How to display data from 2 tables in JSP page
<p>I am new to Hibernate and Spring MVC. I am creating a web app where I want to display a list of all phone numbers and the user's name that is assigned to each number. There are 2 tables - one for numbers and another for users. Each indexed by id. The user id is a foreign key in the numbers table. When I try to display the name in a JSP page, all I get is the id representation of the user instead of the name value when I return a List of numbers. Is there a standard hibernate CRUD example that would create the List to include the user's name value instead of its ID. representation? if not, can someone help me create the right query procedure for this?</p>

<p>I am using Java 1.8, Spring 4.2.5, MySQL 5.7 and Hibernate 5.0.1 along with Tomcat 8 using Eclipse Mars IDE. </p>

<p>Entity Classes:</p>

<pre><code>@Entity
@Table(name="Numbers")
public class Numbers {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name="id", unique=true, nullable=false)
    private Long id;
    @Column(name="Number", nullable=false) 
    private String Number;  
    @Column(name="UserID", nullable=false)
    private Long UserID;    
    @ManyToOne
    @JoinColumn(name="FK_NUMBERS_USER")
    private Users Username;

//getters &amp; setters
}

@Entity
@Table(name="Users")
public class Users {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name="id", unique=true, nullable=false)
    public Long id;

    @Column(name="Username", unique=true, nullable=false)
    public String Username;

        //getters &amp; setters
}
</code></pre>

<p>Controller Code:</p>

<pre><code>@RequestMapping(value="/numbers", method = RequestMethod.GET)
    public ModelAndView getAllNumbers() {

        List&lt;Numbers&gt; allNumbers = numbersService.findAllNumbers();

        ModelAndView model = new ModelAndView("numbers");
        model.addObject("numbers", allNumbers);

return model;
}
</code></pre>

<p>Service Interface:</p>

<pre><code>public interface NumbersService {

    void saveNumber(Numbers number);
    void deleteUser(Numbers number);
    List&lt;Numbers&gt; findAllNumbers();

}
</code></pre>

<p>JSP File c:foreach:</p>

<pre><code>table border="1"&gt;
             &lt;tr&gt;
                &lt;th&gt;Number&lt;/th&gt;
                &lt;th&gt;Username&lt;/th&gt;    
             &lt;/tr&gt;
             &lt;tbody&gt;
                &lt;c:forEach items="${numbers}" var="numbers"&gt;
                    &lt;tr&gt;
                        &lt;td&gt;${numbers.id}&lt;/td&gt;
                        &lt;td&gt;${numbers.number}&lt;/td&gt;
                        &lt;td&gt;${numbers.username}&lt;/td&gt;
                    &lt;/tr&gt;
                 &lt;/c:forEach&gt;
              &lt;/tbody&gt;            
            &lt;/table&gt;
</code></pre>

<p>Console Output from controller when processing query for numbers:</p>

<pre><code>Number ID     Number      User ID
1           12055551212    1 
2           12065551212    2 
3           12075551212    1 
</code></pre>

<p>I can't get the get the same output on the JSP page due to "userid" not a property of the Numbers class and if use numbers.username in the JSP the table filed is blank. How do I get the names in the JSP file.?</p>

<p>DAO Implementation:</p>

<pre><code>package com.voip.service.impl;


import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;

import com.voip.dao.NumbersDAO;
import com.voip.domain.Numbers;
import com.voip.domain.Users;
import com.voip.service.NumbersService;


public class NumbersServiceImpl implements NumbersService {


    @Autowired
    private NumbersDAO numbersDAO;


    @Override
    public List&lt;Numbers&gt; findAllNumbers() {

        List&lt;Numbers&gt; allNumbers = (List&lt;Numbers&gt;) numbersDAO.findAll();
        return allNumbers;


    }

}
</code></pre>