I am a little confused about the usage of entity vs session beans. I am aware that entity beans represent an object view of data in the database. But can they be used to do database insert/update/delete?
Also can session beans(both stateful and stateless) be used for insert/update/delete?
Also, how does a request reach the EJB from the servlet? I know that the servlet has a HttpRequest and HttpResponse object - so does the request object bring the query with it for processing by EJB?
Yes! entity bean can be used to update,insert and delete. Suppose you have an entity bean Employee. Insert:- When we call EmployeeHome.create("xyz",123) a new row is inserted in Employee table. Update:- When we call Employee.setName("abc") the name will be updated to abc. Delete:- when we call Employee.delete
We can session bean for database related operations, create a connection pass SQL string (Like normar JDBC) but its not an standard practice.
Meghna, The servlet calls the session bean. If you have an EJB, the session bean calls the EJB. If not the session bean calls a DAO (data access object) that updates the database. This DAO could use JDBC, Hibernate, EJB or any technology of your choice.
The request is typically more of a business request like "I want to add $5 to my account". The DAO layer is responsible for creating the query. You want the database logic to be as centralized as possible. The web layer shouldn't care what persistence technology you are using.
Thanks for responding. Just a few follow on questions: 1. When you say servlet calls the session bean - do you mean that the actual code for the call is in the JSP (and the servlet is automagically generated, which in effect ends up calling the session bean)?
2. Also, when you say that the session bean calls the EJB - I presume you mean Entity bean and not just any Enterprise bean?
3. So what I understand is that session bean typically never does any update/insert/delete to database? In the presence of entity bean it happens here using some pesistence technology, otherwise the session bean itself can call DAO(is that same as a persistence technology?)as you explained?
Sorry for so many questions, just want to get this straight in my head!
Meghna, 1) No. I meant that you write a servlet which calls the session bean and then forward to the JSP. The JSP is the presentation layer and not should be making any calls to the back end (EJB) layer. 2) Yes I did. Sorry for the ambiguity on that. 3) By definition, you need some persistence technology. JDBC happens to be one built in with Java and entity beans (JPA in EJB 3) are built in with Java Enterprise Edition. The session bean can call these persistence technologies directly. However, it is better practice put the persistence technology calls in their own object - called a DAO.
Questions are good!
Joined: Jun 08, 2007
Thanks very much Jeanne, sorry to send you a mail. But after waiting for 2 days and still no reply I was not sure what to do.