Entity Beans  «Prev  Next»

Lesson 2 Creating an entity bean instance
ObjectiveWrite the code to create an entity bean instance.

Connecting to the Home Object in Jave EE

In Jakarta EE 9 and later, Entity Beans and EJB 2.x Home interfaces are considered obsolete. Instead, Jakarta EE encourages the use of JPA (Jakarta Persistence API) for entity management and dependency injection or JNDI lookup (in special cases) for component access.
Here's how the original J2EE-era example would be rewritten for Jakarta EE 9 (2025) using modern patterns with JPA and CDI (Contexts and Dependency Injection).
✅ Rewritten Version (Jakarta EE 9, 2025)
📌 Connecting to the Entity (JPA-managed) Instead of looking up a "home object" via JNDI, we work with JPA entities using the `EntityManager`.
import jakarta.inject.Inject;
import jakarta.persistence.EntityManager;
import jakarta.transaction.Transactional;

public class CustomerService {

    @Inject
    private EntityManager em;

    @Transactional
    public Customer createCustomer(Long custNum) {
        Customer customer = new Customer();
        customer.setCustNum(custNum);
        // Name is not yet set
        em.persist(customer);
        return customer;
    }
}

🔍 Note: `EntityManager` is injected using `@Inject` (or `@PersistenceContext` in older code). Transactions are handled with `@Transactional`.
📌 Customer Entity (JPA Example)
import jakarta.persistence.Entity;
import jakarta.persistence.Id;

@Entity
public class Customer {

    @Id
    private Long custNum;

    private String name;

    // Getters and Setters
    public Long getCustNum() { return custNum; }
    public void setCustNum(Long custNum) { this.custNum = custNum; }

    public String getName() { return name; }
    public void setName(String name) { this.name = name; }
}


📌 Summary of Major Changes (J2EE → Jakarta EE 9)
J2EE (Old) Jakarta EE 9+ (Modern)
InitialContext and lookup() Dependency Injection (@Inject)
PortableRemoteObject.narrow() No longer required
Entity Beans (CustomerHome, etc) Replaced by POJOs + @Entity and JPA
Manual EJB lifecycle management Managed via EntityManager and container
create() on Home interface em.persist() for new entities
J2EE vs Jakarta9
J2EE vs Jakarta9


Creating a new entity

The entity bean maps to the underlying persistent data. To create some persistent data, such as a new customer, the client creates a new instance of the Customer bean. The new instance automatically creates the underlying data. In practice, the persistent data may be created by another application or bean. You should only create a bean when your client is responsible for creating a new entity object. The code in the client is very similar to the creation of session beans. In this case the bean instance and the underlying row are created. The row contains only the custnum. The name is not set yet. The create() method returns the remote reference to the just-created EJBObject of the instance, called mickey in the example below.

// Connecting to the Home Object in Jakarta EE 9

// Jakarta EE has streamlined bean lookup through CDI and JNDI.
// Assuming 'Customer' is now defined as an entity managed by JPA and exposed via CDI or a remote interface.

import jakarta.naming.Context;
import jakarta.naming.InitialContext;
import jakarta.naming.NamingException;

Context initialContext = new InitialContext();
CustomerService customerService = (CustomerService) initialContext.lookup("java:global/myapp/CustomerService");

// Creating a new entity

// In modern Jakarta EE, entity creation is typically managed by services (EJB Stateless beans, CDI beans, or REST endpoints).
// CustomerService is a service bean responsible for managing Customer entities.

Customer mickey = customerService.createCustomer("111");

// The corresponding CustomerService interface could look like:

import jakarta.ejb.Remote;

@Remote
public interface CustomerService {
    Customer createCustomer(String customerNumber);
    Customer findCustomerById(String customerId);
}




The next lesson will discuss, How to find an entity object by using its primary key?

SEMrush Software