Originally posted by saahil sinha:
well so whats the diff between session bean and entity bean writing the data to a database.both require to have jdbc calls written in them
First off, this is not necessarily true because with CMP no SQL needs to be written by the developer.
Second, the difference is in the intention. A Session Bean (in this case I am referring mainly to Stateless Session Beans) is intended primarily to encapsulate business logic. They are great for abstracting service-level API for clients that hide the complexity of the system.
An Entity Bean is intended to represent an object view of some persistent storage that is easily manipulated in
Java. Entity Beans are good (especially CMP) for relatively simple O/R Mapping, though they tend to break down in face of complex domain models.
One of the nice things about Entity Beans over Session Beans + JDBC, is that the
EJB Container handles O/R Mapping (in case of CMP) and data caching which can get pretty nasty if you do in yourself in the Session Bean layer. However, if you can't use the caching features (for example if external applications can modify the database) and don't want to use CMP then Entity Beans lose their luster.
BTW, it is considered bad practice to expose Entity Beans to the clients. Therefore, all Entity Bean access is typically wrapped with Session Beans. This is called the
Session Facade Pattern and is a J2EE Best Practice. Therefore, it is not really a question of Session Beans vs. Entity Beans, rather it is a question of Session Beans + JDBC vs. Session Beans + Entity Beans.