无法刷新未管理的对象:com.app.restaurant.data.Country [countryID = null]

时间:2016-02-29 11:25:59

标签: java ejb

该图像包含两个方法,我想将一个ID从Country表传递到省表

enter image description here

public class DataGeneraterServlet extends HttpServlet {
@EJB
DataGenerator gen;
EntityManager em;
private static final Logger log = Logger.getLogger(MainUtil.class.getSimpleName());

protected void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    response.setContentType("text/html;charset=UTF-8");
    PrintWriter out = response.getWriter();
    out.println("Running Data Generator Servlet");
    try  
    {
        Country c = new Country();
        Province p = new Province();
      gen.addCountry();
      gen.addProvince(c);
      gen.addCity(p);

    }
    catch(Exception e)
    {
        log.log(Level.OFF,"Couldnt Add to database",e);

    }
}
 @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    processRequest(request, response);
}

/**
 * Handles the HTTP <code>POST</code> method.
 *
 * @param request servlet request
 * @param response servlet response
 * @throws ServletException if a servlet-specific error occurs
 * @throws IOException if an I/O error occurs
 */
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    processRequest(request, response);
}

/**
 * Returns a short description of the servlet.
 *
 * @return a String containing servlet description
 */
@Override
public String getServletInfo() {
    return "Short description";
}// </editor-fold>

}

1 个答案:

答案 0 :(得分:0)

Country c = new Country(); //created a new country object but not have an id
Province p = new Province();
gen.addCountry(); //if this method returns a country object it will have an id.
gen.addProvince(c); 
gen.addCity(p);

所以

Country c =  gen.addCountry();
Province p = gen.addProvince(c);
gen.addCity(p);

将给出您的预期结果。

PS:您还必须重构EJB方法以返回持久值。