Entity Bean provides an object-oriented abstraction of relational databases, which can benefit from both object-oriented and process-oriented technologies. Since entity beans and session beans are both enterprise beans, they must have some similarities: entity beans must also have a home interface, bean interface, and Bean class, as shown in:
Session Bean calls the home interface and bean interface of Entity Bean to create, search for, and delete entity beans. Strictly speaking, other clients can also directly call these interfaces, but this is not a good practice.
Home interface:
The home interface of entity bean can be used to create a new entity bean, delete an entity bean, and find an existing entity bean. When you call the create method of the home interface, a new entity bean instance is created, which is similar to the constructor in a general Java class. When you call the Remove Method of the home interface, the specified entity bean instance is deleted. When you call the find method of the home interface, an appropriate entity bean instance is searched and returned. The entity bean must have at least one find method called the findbyprimarykey. Like Session Bean, the home interface of Entity Bean must inherit the ejbhome interface.
Bean interface:
Similar to Session Bean, the bean interface of Entity Bean is also used to expose a bean method, but most of its methods are set and get methods in the domain of Entity Bean. Like Session Bean, the bean interface of Entity Bean must inherit the ejbobject interface.
Bean class:
The bean class of Entity Bean must be integrated with the entitybean interface. It contains the setter and getter methods specified in the bean interface and the methods that contain the business logic. In addition, it must also implement the Declaration cycle methods called by the EJB container, such as ejbcreate.
Container-managed persistence (CMP ):
Entity Bean is related to a persistent data storage in the background. This data storage is usually a relational database. One feature of Entity Bean is to automate this persistence process, this feature is called container management persistence (CMP ). Therefore, when we call the create method of home interface, we not only create a new entity bean, but also add a row of records in the corresponding database table, the entire process is managed by containers for us, including when to insert this row of records; when the find method is called, appropriate records will be searched in the corresponding database table, each row records an instance of an entity bean. When CMP is used, the database mode directly reflects the design of Entity Bean, which is very similar to that in hibernate. Generally, a table corresponds to an entity.
In addition to CMP, another policy is BMP (bean-managed persistence), which requires us to manage all the persistence processes on our own. BMP is usually used in the following situations:
1. Construct an EJB application based on an existing database. If the design of Entity Bean cannot match the existing database pattern, BMP is used.
2. The database does not exist. Entity Bean is used to encapsulate an external system.
3. The EJB server does not support CMP.
An entity bean requires that one or more of its fields be uniquely identified. These fields are called primary keys, just like the ID attribute of each persistence class in hibernate, the primary key is used to find a specific entity bean. It can be any serialized object, such as string or integer. Entity Bean also features eql (EJB Query Language), just like hql in hibernate.