我尝试显示数据库中的数据,但出现 500 错误页面:javax.ejb.EJBException。我不明白为什么它不起作用。也许我的 Bean 错了?请帮我。
Glassfish 日志中的警告:警告:警告:EJB5184:在调用 EJB SearchBean 期间发生系统异常,方法:public java.util.Collection com.stark.logic.SearchBean.searchByNumber(int)
@Entity
@Table(name = "buy")
@XmlRootElement
@NamedQueries({
@NamedQuery(name = "Buy.findAll", query = "SELECT b FROM Buy b"),
@NamedQuery(name = "Buy.findByIdBuy", query = "SELECT b FROM Buy b WHERE b.idBuy = :idBuy"),
@NamedQuery(name = "Buy.findByNumberBuy", query = "SELECT b FROM Buy b WHERE b.numberBuy = :numberBuy")})
public class Buy implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Basic(optional = false)
@Column(name = "id_buy")
private Integer idBuy;
@Basic(optional = false)
@NotNull
@Column(name = "number_buy")
private int numberBuy;
@OneToOne(cascade = CascadeType.ALL, mappedBy = "buy")
private Product product;
@OneToOne(cascade = CascadeType.ALL, mappedBy = "buy")
private Client client;
//getters & setters
EJB 豆:
@Stateless
@LocalBean
public class SearchBean {
@PersistenceUnit
EntityManagerFactory emf;
EntityManager em;
public Collection searchByNumber(int number){
em = emf.createEntityManager();
Query searchQuery = em.createNamedQuery("Buy.findByNumberBuy");
searchQuery.setParameter("number_buy", number);
List list = new ArrayList();
list = searchQuery.getResultList();
return list;
}
}
小服务程序:
@EJB
private SearchBean searchBean;
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
PrintWriter out = response.getWriter();
response.setContentType("text/html;charset=UTF-8");
String num = request.getParameter("number");
int number = Integer.parseInt(num);
List list = new ArrayList();
list = (List) searchBean.searchByNumber(number);
Iterator it = list.iterator();
while(it.hasNext()){
Buy buy = (Buy) it.next();
out.println(buy.getProduct());
}
}