• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

In session bean insert data to DB by using JPA

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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>");
}


}

 
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can you once check the way your Entity bean (i.e) Category is defined
 
Peiyi Xiao
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Ranch Hand
Posts: 38
Eclipse IDE Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic