How to display data from 2 tables in JSP page

时间:2016-05-15 18:46:22

标签: mysql hibernate jsp

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?

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.

Entity Classes:

@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 & 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 & setters
}

Controller Code:

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

        List<Numbers> allNumbers = numbersService.findAllNumbers();

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

return model;
}

Service Interface:

public interface NumbersService {

    void saveNumber(Numbers number);
    void deleteUser(Numbers number);
    List<Numbers> findAllNumbers();

}

JSP File c:foreach:

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

Console Output from controller when processing query for numbers:

Number ID     Number      User ID
1           12055551212    1 
2           12065551212    2 
3           12075551212    1 

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.?

DAO Implementation:

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<Numbers> findAllNumbers() {

        List<Numbers> allNumbers = (List<Numbers>) numbersDAO.findAll();
        return allNumbers;


    }

}

2 个答案:

答案 0 :(得分:2)

这个问题有两个问题。首先,我在@JoinColumn中设置了错误的值。该值应该引用我试图从中获取数据的表的主键。在我的情况下,Numbers表正在尝试访问Users表,因此引用应该是“id”字段。

@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name="userid")
private Users userID;

其次,在我的JSP文件中,我需要确保引用正确的变量来检索数据。在这种情况下,变量将是userID。此外,引用区分大小写。 JSP文件应该有这个引用。

<td>${numbers.userID.username}</td>

这允许我在JSP文件中显示Number和User Name值,这是我想要的。

答案 1 :(得分:0)

  1. 在ManytoOne关系中添加@ManyToOne(fetch = FetchType.EAGER)

  2. 同样在JSP中,获取用户名,更改 $ {numbers.username}到$ {numbers.username.username}