gpt4 book ai didi

java - 无法刷新非托管对象: com. app.restaurant.data.Country[countryID=null]

转载 作者:太空宇宙 更新时间:2023-11-04 12:52:22 25 4
gpt4 key购买 nike

图像包含两种方法,我想将 id 从 Country 表传递到 Province 表

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>

}

最佳答案

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 方法以返回持久值。

关于java - 无法刷新非托管对象: com. app.restaurant.data.Country[countryID=null],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35698334/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com