aspose file tools*
The moose likes EJB and other Java EE Technologies and the fly likes Session vs Entity 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 "Session vs Entity bean " Watch "Session vs Entity bean " New topic
Author

Session vs Entity bean

saahil sinha
Ranch Hand

Joined: Apr 07, 2003
Posts: 68
Hi ,
I would like to know when is an entity bean used and when a session bean is used
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
There are many ways to use EJB concepts, so I hedge my bets with "usually" in many places. You can certainly do other things, too!
Entity beans represent concrete things in your business object model - people, places, things. In my business: users, customers, phone call records, contracts, etc.
Entity beans are (usually, and sometimes automatically) persisted to a database or some other permanent store, so they represent things with long lives.
Session beans usually do some processing on one or more Entity beans. For example an account trasfer bean might debit money from one account bean and credit it to another. They can be stateful, too, but only for the length of the session (hence the name) like maybe a shopping cart that exists only for the time you are shopping.
Now some OO modelers hate this idea, but an Entity bean can also represent a row in a database. You can even imagine the Entity beans ARE the permanent data store, because the load and save stuff is automated or hidden. If you create a new bean, you insert a new row. If you modify one, you update a row.
To come from another angle, it *sometimes* works to see your Entity beans as business things that can be used across many applications, and session beans as application-specific logic applied to the business things.
Hope that helped! Let me know if it raised more questions than answers.


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Andres Gonzalez
Ranch Hand

Joined: Nov 27, 2001
Posts: 1561
saahil,
Just think of an entity bean as a Noun in your application.Just like stan wrote, people, things, etc..
Think of a Session Bean as a verb. Actions that might occur in your application.

hope this helps


I'm not going to be a Rock Star. I'm going to be a LEGEND! --Freddie Mercury
aaron chang
Greenhorn

Joined: Mar 21, 2003
Posts: 18
hi,
entity beans are about data, while session beans are about processing on the data (for stateful session beans), or just about processing (for stateless beans).


SCJP,SCWCD
Parsuram panigrahi
Greenhorn

Joined: Feb 18, 2003
Posts: 17
Session bean is for business logic where as entity bean is used for data. You can represent credit card as a entity bean, where as you can use a session bean for a convertor.
From Ed Roman mastering EJB2.0 page 62
Session beans. Session beans model business processes. they are like verbs because they are actions. The action could be anything, such as adding numbers, accessing a database, calling a legacy systems, or calling other enterprise beans. Examples include a pricing engine, a workflow engine, a catalog engine, a credut card authorizer, or a stock trading engine.

Entity beans. Entity beans model business data. They are like nouns because they are data objects-that is, java objects that cache database information. Examples include a product, an order, an employee, a credit card, or a stock. Session beans typically harness entity beans to achieve business goals such as a stock-trading engine(session bean) that deals with stocks 9entity beans).
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
I think a lot of people use the "noun" and "verb" kind of criteria, but not without controversy.
An Entity Bean is a full-fledged object and is allowed to have behavior which is definitely verbish. An Account Entity Bean might have business rules like reporting deposits over 10,000 to the IRS or any appropriate business logic.
There are two schools of thought illustrated here. One believes an app server should have a rich, meaningful business domain object model and the other just makes a data access object per table and pumps data back and forth between the database and the business function layer.
Either one works with trade-offs for performance, extensibility, reusability, etc. I've said in several threads: Know what is important to you and focus on it. Mix such values half-and-half and you're begging for trouble.
lalitha sundaresan
Greenhorn

Joined: May 12, 2003
Posts: 26
Every entity bean instance represents an underlying record in a table.
they are persistant. they can be reused.
they are not removed after use.( ebcuse they ahve some useful info in them which can be used later)
but session bean instances are availble on an one to one basis, ie one session bean instance for one client request , after the request is over they are removed . for every new requests one session bean is created. as the name suggests it is used only for one session. then for every new session another instance has to be created.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Session vs Entity bean