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
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).
Joined: Jan 29, 2003
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.
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.