Join Patrick Royal for an in-depth discussion in this video Session vs. entity objects, part of JavaBeans Essential Training.
- In this video, we're going to investigate the differences between session and entity objects within enterprise JavaBeans. As the name suggests, a session bean is created within a specific client session by that client. Although more than one client can share a session bean, the bean will only last as long as its client. Statefull session beans will retain a state between method calls, while stateless session beans retain no state at all. The state that a statefull session bean maintains is called its conversational state because it is maintaining a connection with one or more clients.
When a client session ends, whether intentionally or due to a server crash, the bean disappears. When it is instantiated again, the new instantiation will not remember the state of the previous instance. On the other hand, an entity bean is designed to be persistent across multiple clients and instantiations. It maintains its state through a unique primary key that persistently identifies the bean. Given a primary key, the client can reconnect to an entity bean if it still exists.
If it is no longer available, then the key can be used to recreate the bean with its persistent data. Entity beans are more commonly used for performing complex tasks for a remote client because there is a possibility that the task will not be completed in a single session. Both session and entity beans are what is known as coarse-grained Java objects. This terminology refers to the way in which the beans interact with raw data. Fine-grained Java objects manage data that directly maps to a table of raw data.
They're essentially just copying data from a data source, operating on it, and then storing the changed data back in that same source. Course-grained Java objects provide an additional layer of abstraction. They manage persistent data that is stored in multiple fine-grain Java objects. This makes it easier to deal with more complex data, and helps to prevent the duplication of work if data is to be accessed multiple times. Deciding whether a bean should be considered a session or an entity bean in eclipse is very easy.
If your programming a session bean, then it will extend the javax.ejb.SessionBean class. If you're programming an entity bean, then it will extend the javax.ejb.EntityBean class. The built in methods contained within each of these classes will help to define the different behavior of the two types of Beans. Now that we have an idea of what session and entity objects are, in the next video we'll create and use a sample session Bean.
- Building your first simple JavaBeans component
- Creating simple entity beans and message-driven beans
- Defining a primary key
- Understanding the life cycle of beans
- Creating home and component interfaces
- Controlling concurrent access to JavaBeans objects
- Debugging and optimizing JavaBeans code
- Enabling security and encryption
- Creating asynchronous methods