File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes EJB and other Java EE Technologies and the fly likes In session bean insert data to DB by using JPA Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » EJB and other Java EE Technologies
Bookmark "In session bean insert data to DB by using JPA" Watch "In session bean insert data to DB by using JPA" New topic
Author

In session bean insert data to DB by using JPA

Peiyi Xiao
Greenhorn

Joined: Feb 06, 2013
Posts: 4
In a servelt, I try to call the session bean method to insert data to database via JPA. The insert process is written in the session bean.

I tried another example, which I select data from DB. The "select"-getCategory() works good. But I have no idea that why insert-insertData() does not work.

The error information is:

HTTP Status 500

description: The server encountered an internal error () that prevented it from fulfilling this request.

exception: javax.ejb.EJBException

note: The full stack traces of the exception and its root causes are available in the GlassFish Server Open Source Edition 3.0.1 logs.


I think there is something wrong with "tx.commit()", when I comment it then there is no error. But I do not know what the exactly problem.

Here is the bean class

@Stateless
@LocalBean
public class testSession {

public testSession() {
// TODO Auto-generated constructor stub
}

//get all the rows from the category table
public List<Category> getCategory(){
EntityManagerFactory emf;
EntityManager em;

emf=Persistence.createEntityManagerFactory("test");
em=emf.createEntityManager();

String queryStr="select cat from Category cat";

Query query;
query=em.createQuery(queryStr);
List<Category> cat=query.getResultList();

return cat;

}

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();

}
}

In the 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>");
}


}

Shashank Gollapudi
Ranch Hand

Joined: Jan 19, 2012
Posts: 33
Can you once check the way your Entity bean (i.e) Category is defined
Peiyi Xiao
Greenhorn

Joined: Feb 06, 2013
Posts: 4
Shashank Gollapudi wrote:Can you once check the way your Entity bean (i.e) Category is defined


The Category entity bean is already defined --> it is generated from the DB table.

As I said, I tried "select" like: and in the servlet I do cs.findAllRows(). It works. But I do not know why cs.inserData() does not work.
//////////////////////////////////////////////////////////////////////////////////////
public List<Category> findAllRows(){
EntityManagerFactory emf;
EntityManager em;
List<Category> cat=null;

emf=Persistence.createEntityManagerFactory("PeiyiFoodStore");
em=emf.createEntityManager();

String queryStr="select cat from Category cat";

Query query;
query=em.createQuery(queryStr);
cat=query.getResultList();

em.close();
emf.close();
return cat;
}
//////////////////////////////////////////////////////////////////////////////////////

For the insertData(), if I comment "tx.commit();". Then I run it, there is no error. Of course, the transaction also wont happen. I do not what is the problem and how to fix it.
Mahendr Shinde
Ranch Hand

Joined: Sep 03, 2011
Posts: 38

Why are you trying BMT?
If your session bean method is responsible for data manipulation, you better use JTA and Resource injection for EntityManager. This will allow you to use CMT. In CMT beans, container creates, commits transaction of your behalf.
In case you want to use BMT (Bean managed Transactions), your code where you trying to save new record must be enclosed in try...catch block



This will give you proper error (in server console) if record could not be saved.


There is still lot to learn!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: In session bean insert data to DB by using JPA
 
Similar Threads
Exception in deployment of ejb ear
JPA Transaction management
not able to insert data in db using jpa(hibernate as provider) and jboss AS
JPA with JSF
How to create a Template for EntityManager in a WebApplication