Java Reference
In-Depth Information
The following code shows an example of a stateless session bean to save an entity
in the database:
public class ExampleOfSessionBean {
@PersistenceContext EntityManager em;
public void persistEntity(Object entity){
Talking about improvements of session beans, we first note two changes in stateful
session beans: the ability to execute life-cycle callback interceptor methods in a
user-defined transaction context and the ability to manually disable passivation of
stateful session beans.
It is possible to define a process that must be executed according to the lifecycle
of an EJB bean (post-construct, pre-destroy). Due to the @TransactionAttrib-
ute annotation, you can perform processes related to the database during these
phases and control how they impact your system. The following code retrieves an
entity after being initialized and ensures that all changes made to the persistence
context are sent to the database at the time of destruction of the bean. As you can
see in the following code, TransactionAttributeType of init() method is
NOT_SUPPORTED ; this means that the retrieved entity will not be included in the per-
sistence context and any changes made to it will not be saved in the database:
public class StatefulBeanNewFeatures {
EntityManager em;
Search WWH ::

Custom Search