Win a copy of Java Concurrency Live Lessons this week in the Threads forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Session Bean ,Entity Bean persistence  RSS feed

 
saahil sinha
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
if data can be written to a database using session beans and entity beans then why do we say tht entity beans represtn data in persistent storage mechanism.Coz session bean data is also stored in database.
 
Lasse Koskela
author
Sheriff
Posts: 11962
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is meant with "data can be written to a database using session beans" is that the SessionBean code contains raw JDBC or calls other classes that make use of JDBC. It doesn't mean that the state of the SessionBean would be persisted automatically.
An EntityBean's state is persisted to a database either automatically (if using CMP, container-managed persistence) or manually using JDBC (if using BMP, bean-managed persistence).
 
saahil sinha
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Chris Mathews
Ranch Hand
Posts: 2712
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Lasse Koskela
author
Sheriff
Posts: 11962
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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, you must remember that an EntityBean may be CMP or BMP. Only in BMP EntityBeans you actually write SQL. For CMP EntityBeans, the container generates the necessary JDBC stuff for you behind the scenes. In this respect, CMP EntityBeans are clearly different from a SessionBean with JDBC code within.
The other, more fundamental difference is that EntityBeans represent the data instead of providing methods to write/read the data.
SessionBeans with JDBC code provide services such as the following:

EntityBeans provide an object view to the data:
 
saahil sinha
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sorry lasse wht i meant was not diff between entity bean and session bean writing to a database but session bean and bmp ejb coz in both u have to write code to access database.
And also i didnt get ur point regarding entity bean being used to view data rather than read/write data.If its not a problem could u pls provide me an explanation or link to a site where i could understand this better.
 
Lasse Koskela
author
Sheriff
Posts: 11962
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'll try to rephrase a bit.
What I meant with "representing the data" was that the EntityBean is a first class persistent entity instead of "just data" that you write/read to/from a database.
Here's a quote from the J2EE Tutorial:
Typically, each entity bean has an underlying table in a relational database, and each instance of the bean corresponds to a row in that table.
This quote kinda implies that you have an object view of the data in the database (generally, 1 EntityBean equals 1 database row), which abstracts the nitty gritty details of SQL statements from your design model. Objects instead of procedures.
You should also skim through the What is an Entity Bean chapter of the J2EE Tutorial. Better yet, buy a good book on EJBs. A couple of good choices are
- Mastering Enterprise JavaBeans
- Enterprise JavaBeans Component Architecture
- Enterprise JavaBeans
- Applied Enterprise JavaBeans Technology
- Applying Enterprise JavaBeans 2.1
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!