Entity Beans  «Prev  Next»

Lesson 1

Client view of Entity Beans

In Java EE 8, Entity Beans, a legacy feature of EJB, have become optional as of EJB 3.2, with the Java Persistence API (JPA) being the recommended approach for persistence.
Key Changes in EJB 3.2 and Java EE 8:
Example: JPA Entity vs. Legacy Entity Bean
Using JPA (Preferred)
import javax.persistence.*;

@Entity
@Table(name = "users")
public class User {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    private String name;
    private String email;

    // Getters and Setters
}

- This JPA entity is lightweight, uses annotations, and integrates seamlessly with JPA providers like Hibernate.
Legacy Entity Bean (Deprecated)
import javax.ejb.EntityBean;

public class UserEntityBean implements EntityBean {
    private Long id;
    private String name;
    private String email;

    public Long ejbCreate(Long id, String name, String email) {
        this.id = id;
        this.name = name;
        this.email = email;
        return id;
    }

    public void ejbLoad() {}
    public void ejbStore() {}
    public void ejbRemove() {}
    public void ejbActivate() {}
    public void ejbPassivate() {}
}
- This approach requires an EJB container, involves complex lifecycle methods, and is now discouraged.
Conclusion: Entity Beans are now legacy, and Java EE 8 applications should use JPA for persistence while using EJBs for business logic and transactions.

Creating the Entity Bean instance

This module introduces the client side of entity beans. You will be introduced to the lookup of the home interface from JNDI.
Using the home interface, you will see how the bean instance, and the underlying data, can be created.
  • Finding an instance
    For data that exists, you will learn to use the findByPrimaryKey() method of the home interface to locate the data and load it into the bean.
  • Business methods: You will learn to use the EJBObject reference returned by the findByPrimaryKey() to invoke the business methods of the bean and remove the bean.


Customer bean example

The examples in this part of the course use a Customer bean and client. The customer table, which is very simple, is created with the SQL command as follows:
create table customer(
custnum varchar(10) primary key,  
name varchar(25)
);

It has two columns labeled custnum and name. In the real world, it would have more columns. To keep the code simple it uses a Java String for the primary key. In practice, it would be an integer or something more complex. The remote interface of the Customer bean is as follows:
import javax.ejb.EJBObject;
import java.rmi.RemoteException;
public interface Customer extends EJBObject {
public void setName(String name) throws RemoteException;
public String getInfo() throws RemoteException;
// returns custnum and name
}

  • Course Project
    The last part of this module is the course project where you will write the client of an entity bean.
    The next lesson explores creation of an entity bean.

SEMrush Software