Java JPA查询createQuery问题

时间:2015-10-15 22:42:31

标签: java servlets jpa

我正在学习JPA并且一直在学习教程。

教程项目是一个简单的航班预订"带有JPA和derby数据库的动态站点。

我有一个带有@Entity注释的Passenger类和一个Passenger服务类作为无状态本地bean。乘客服务类有一个getPassengers方法,它执行" createQuery(" SELECT p FROM PASSENGER p",Passenger.class);"并且返回的结果是乘客列表。

要调用此方法,我有一个GetPassenger servlet,并将查询结果列表设置为http请求属性,并将其调度到jsp视图文件。

问题在于,当我使用getPassengers方法运行servlet时,即使我确保数据库的乘客表中有多个条目,返回的列表也是空的。以下是我的代码。感谢任何帮助。

感谢。

乘客类:

package com.airline.models;

import java.io.Serializable;
import java.util.Date;

import javax.persistence.*;

/**
* Entity implementation class for Entity: Passenger
*
*/
@Entity

public class Passenger implements Serializable {
@Override
public String toString() {
    return "Passenger [id=" + id + ", firstName=" + firstName + ", lastName=" + lastName + ", dob=" + dob
            + ", gender=" + gender + ", flightclass=" + flightclass + "]";
}

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Integer id;

private String firstName;

private String lastName;

@Temporal(TemporalType.DATE)
private Date dob;

@Enumerated(EnumType.STRING)
private Gender gender;

@Enumerated(EnumType.STRING)
private FlightClass flightclass;

PassengerService类:

package com.airline.service;

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.airline.models.Flight;
import com.airline.models.Passenger;

/**
*  Session Bean implementation class PassengerService
*/
@Stateless
@LocalBean
public class PassengerService {

/**
 * Default constructor.
 */
public PassengerService() {
    // TODO Auto-generated constructor stub
}

@PersistenceContext(unitName = "airline")
EntityManager em;

public void addPassenger(Passenger p) {
    em.persist(p);
}

public List<Passenger> getPassengers() {
    try{

    TypedQuery<Passenger> query = em.createQuery("SELECT p FROM PASSENGER p",    Passenger.class);

    List<Passenger> pList = (List<Passenger>)query.getResultList();

    return pList;
    }
    catch (Exception e)
    {
        System.out.println("Passenger not found");
        return null;
    }
}
}

GetPassenger servlet:

package com.airline.controllers;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.ejb.EJB;
import javax.print.attribute.standard.PrinterState;
import javax.servlet.RequestDispatcher;
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.airline.models.Flight;
import com.airline.models.Passenger;
import com.airline.service.FlightService;
import com.airline.service.PassengerService;

/**
* Servlet implementation class GetFlight
*/
@WebServlet("/GetPassenger")
public class GetPassenger extends HttpServlet {
private static final long serialVersionUID = 1L;


@EJB
PassengerService ps;
/**
 * @see HttpServlet#HttpServlet()
 */
public GetPassenger() {
    super();
    // TODO Auto-generated constructor stub
}

/**
 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
 */
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    List<Passenger> pList = (List<Passenger>)ps.getPassengers();

    request.setAttribute("Passenger_List", pList);

    if(pList == null)
    {
        System.out.println("passenger list empty");
        return;
    }

    RequestDispatcher view = request.getRequestDispatcher("WEB-INF/views/passenger_list.jsp");

    view.forward(request, response);


}

/**
 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
 */
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    // TODO Auto-generated method stub
    doGet(request, response);
}

1 个答案:

答案 0 :(得分:0)

管理通过更改&#34; SELECT p FROM PASSENGER p&#34;中的实际查询来修复它。 to&#34; SELECT p FROM Passenger p&#34;。

Passenger是指实体类的名称而不是表的名称。?