在servelt中,我嘗試調用會話bean方法通過JPA將數據插入數據庫。插入過程寫入會話bean中。使用會話bean在JPA中插入數據
我試過另一個例子,我從數據庫中選擇數據。 「選擇」運作良好。但我不知道爲什麼插入不起作用。
錯誤信息是:
HTTP狀態500
描述:服務器遇到阻止其完成此請求一個內部錯誤()。
異常:javax.ejb.EJBException異常
注:異常的完整堆棧跟蹤和其根源是在GlassFish Server開源版3.0.1日誌可用。
我認爲「tx.commit()」有問題,當我評論它時,則沒有錯誤。但我不知道究竟是什麼問題。
這裏是bean類
@Stateless
@LocalBean
public class testSession {
public testSession() {
// TODO Auto-generated constructor stub
}
public void insertData(){
EntityManagerFactory emf;
EntityManager em;
//the Entity Class-Category
Category cat=new Category();
//set value
cat.setId(5);
cat.setName("test cat");
//the "test" is the persist unit in persistence.xml
emf=Persistence.createEntityManagerFactory("test");
em=emf.createEntityManager();
EntityTransaction tx=em.getTransaction();
tx.begin();
em.persist(cat);
tx.commit();
em.close();
emf.close();
}
}
在servlet
@WebServlet("/testServlet")
public class testServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
@EJB
testSession ts;
public testServlet() {
super();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
PrintWriter out=response.getWriter();
out.print("<html><body>");
//call the method in the session bean to insert data
ts.insertData();
out.print("</body></html>");
}
}
請編輯您的問題,併發布全stractrace爲了找到罪魁禍首和可能的方案。 –