Bean Managed Persistence   «Prev  Next»

Lesson 1

The (BMP) Bean-managed Persistent Entity Bean

This modules covers writing the code for the entity bean, including the home interface, the remote interface, and the session bean class and the deployment descriptor.

Bean-managed persistence (BMP)

The data-access protocol for transferring the state of the entity between the entitybean instances and the underlying database is referred to as object persistence. In the last few sections we looked at the container-managed persistence model, wherein entity beans rely on the container to provide the persistence logic. The EJB specifications also support entity beans with bean-managed persistence (BMP) wherein the entity beans write explicit code to access the persistence store. For most users, accessing the persistence store involves writing JDBC code to access a relational database.
From a bean provider's perspective, this is more effort than the CMP model, because he or she must provide the logic to create, update, and remove the bean's state. However, bean-managed persistence is a viable choice in some circumstances. One benefit of BMP comes when mapping a bean's attributes to database fields proves to be difficult. This may occur if the bean state is defined by data in different databases. It can also occur if you are using a target data store that is a legacy system. You will probably need to access the store using a vendor-specific protocol rather than SQL commands, and the EJB container might not support the protocol. Bean-managed persistence is a good alternative to container-managed persistence when the container tools are inadequate for mapping the bean’s instance state to the underlying data source.
One major disadvantage of using a BMP (other than that it makes more work for the bean provider) is that it ties the bean to a specific database type and structure. Any change in the database schema will require a coding change in the bean class and it may not be a trivial change. With CMP, it is managed by the abstract persistence schema and thus is easier to accommodate.


Jakarta EE 9 does not support "Entity Beans" (CMP/BMP) from the older EJB (Enterprise JavaBeans) specification. Entity Beans were officially deprecated in Jakarta EE 8 and completely removed in Jakarta EE 9 when the platform underwent major changes, including the package namespace migration from javax.* to jakarta.*.
Answer to Your Questions:
  1. Create and Find Methods
    • These were used in Entity Beans (CMP/BMP) with EJB 2.x.
    • In Jakarta EE 9, Entity Beans no longer exist. Instead, developers use Jakarta Persistence (formerly JPA - Java Persistence API) for database interactions.
    • With JPA, you define entities using annotations (@Entity) and use the EntityManager API to persist, find, and manage objects.
    • Example: Instead of findByPrimaryKey, you now use EntityManager.find(Class<T>, Object primaryKey).
  2. Remote Interface and Business Methods
    • The concept of Remote Interfaces (@Remote) and Business Methods (@Local) still exists in stateless/stateful session beans under EJB 3.x and Jakarta EE 9.
    • However, since Entity Beans are removed, they do not have business methods anymore.
    • Instead, persistence logic is typically placed in stateless session beans (SLSB) that interact with JPA entities.

Alternative Approach in Jakarta EE 9:
  • Use JPA (jakarta.persistence.*) for entity management.
  • Use CDI (jakarta.enterprise.context.ApplicationScoped) or Stateless Session Beans (@Stateless) for business logic.
  • Use Jakarta RESTful Web Services (jakarta.ws.rs.*) for exposing APIs.

Example of a Modern Equivalent:
@Stateless
public class UserService {

    @PersistenceContext
    private EntityManager entityManager;

    public void createUser(User user) {
        entityManager.persist(user);
    }

    public User findUser(Long id) {
        return entityManager.find(User.class, id);
    }
}


Entity Class:
@Entity
@Table(name = "users")
public class User {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    private String name;
    // Getters and Setters
}

Conclusion:
  • Entity Beans (EJB CMP/BMP) are obsolete and removed in Jakarta EE 9.
  • Create and Find methods are replaced by EntityManager.persist() and EntityManager.find().
  • Remote interfaces and business methods still exist for session beans but not for entity beans.
  • Use JPA and CDI instead of old Entity Beans for persistence and business logic.

The first part of the module examines how to create an entity object and how to find it once created. The next part covers the remote interface for the entity bean and the way that it invokes the business methods. You will find this is very similar to that of the session bean.


 

Container callbacks Jakarta EE 9

Container callbacks still exist in Jakarta EE 9, although some specific callbacks related to the removed EJB CMP (Container-Managed Persistence) and BMP (Bean-Managed Persistence) entity beans are, of course, gone. The broader concept of lifecycle callbacks for various components like servlets, listeners, and other managed beans remains. Here's a breakdown:
  • Servlets: ServletRequestListener, ServletContextListener, ServletRequestAttributeListener, and related listeners and their associated callback methods (e.g., contextInitialized, requestInitialized, etc.) are still very much in use. These are fundamental to web application development.
  • Listeners: Other listener types and their callbacks, such as those for JMS (Java Message Service), CDI (Contexts and Dependency Injection), and other specifications, continue to function as expected.
  • CDI Lifecycle Events: CDI defines its own set of lifecycle events and observer methods using annotations like @Observes, @BeforeInstall, @AfterInstall, etc. These are crucial for managing bean lifecycles and remain relevant in Jakarta EE 9.
  • EJB Session Beans: While entity beans have changed significantly, session beans do still support lifecycle callbacks. Annotations like @PostConstruct, @PreDestroy, @Activate, and @Passivate are still used for managing the lifecycle of stateless, stateful, and singleton session beans.
  • Jakarta Persistence (JPA): JPA, which is the modern way to handle persistence, uses its own set of lifecycle annotations like @PrePersist, @PostPersist, @PreUpdate, @PostUpdate, @PreRemove, @PostRemove, @PostLoad. These are how you hook into the persistence lifecycle and are essential for tasks like auditing, validation, and other operations.

The behavior of the container callbacks differs from that of session beans. You will be introduced to the differences in this module. Loading and storing the data from the persistent store is activated through the callbacks ejbLoad() and ejbStore().You will see how and when they are used. The course does not examine the JDBC code that is required to make the load and store function work with RDBMSs. The JDBC [1](Java Database Connectivity package) methods are treated as black boxes.

The next lesson introduces the entity bean home interface and the create() methods.
[1]JDBC: Java database connectivity package that is used by Java programmers to access RDBMS.

SEMrush Software