wood burning stoves 2.0*
The moose likes EJB and other Java EE Technologies and the fly likes Confusion between Entity and Session Bean Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » EJB and other Java EE Technologies
Bookmark "Confusion between Entity and Session Bean" Watch "Confusion between Entity and Session Bean" New topic
Author

Confusion between Entity and Session Bean

Meghna Bhardwaj
Ranch Hand

Joined: Jun 08, 2007
Posts: 109
HI All,

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?

Many thanks.
vivek kumar
Greenhorn

Joined: Sep 18, 2007
Posts: 13
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.
Jeanne Boyarsky
author & internet detective
Marshal

Joined: May 26, 2003
Posts: 30758
    
156

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.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Meghna Bhardwaj
Ranch Hand

Joined: Jun 08, 2007
Posts: 109
HI Jeanne,

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!

Thanks.
[ October 10, 2007: Message edited by: Meghna Bhardwaj ]
Jeanne Boyarsky
author & internet detective
Marshal

Joined: May 26, 2003
Posts: 30758
    
156

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!
Meghna Bhardwaj
Ranch Hand

Joined: Jun 08, 2007
Posts: 109
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.

Many thanks once again!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Confusion between Entity and Session Bean