An Introduction to the Enterprise JavaBeans 3.0 (EJB 3) Specification

Entity Beans

An EJB 3.0 entity is a lightweight persistent domain object. Entity beans are marked with the @Entity annotation, and all properties/fields in the entity bean class not marked with the @Transient annotation are considered persistent. Entity bean persistent fields are exposed through JavaBean-style properties or just as public/protected Java class fields.

Entity beans can use helper classes for representing entity bean state, but instances of these classes don't have a persistent identity. Instead, their existence is tied strongly to the owning entity bean instance; also these objects are not shareable across entities.

Home interface

Entity beans do not need home interfaces.

Business interface

Entity beans do not need business interfaces. They are optional.

Entity class

  • The entity class must be annotated with the entity annotation or denoted in the XML descriptor as an entity.
  • The entity class must have a no-arg constructor. The entity class may have other constructors as well.
  • The no-arg constructor must be public or protected.

Persistent fields and properties

For single-valued persistent properties, the method signatures are:

  • T getProperty()
  • void setProperty(T t)

Code sample

As you can see from the following code sample, an entity bean is annotated with a @Entity tag. In the sample, we have some member variables and their corresponding getters and setters. Also the code sample shows how to annotate the CMR relationship.

A one-to-many relationship is shown using the @OneToMany tag. In this example, the Customer bean has a one-to-many relationship with the Orderscode> bean (one customer can have multiple orders).

Similarly, Customercode> has a many-to-many relationship with the Phonescode> bean. Some business methods will be defined in the business interface and implemented in the bean, for example, addPhone() which adds a phone record and associates it with the customer:

@Entity
    public class Customer implements Serializable {
      private Long id;
      private String name;
      private Address address;
      private Collection orders = new HashSet();
      private Set phones = new HashSet();
    
      // No-arg constructor
      public Customer() {}
      @Id
      public Long getId() {
        return id;
      }
      public void setId(Long id) {
        this.id = id;
      }
      public String getName() {
        return name;
      }
      public void setName(String name) {
       this.name = name;
      }
      public Address getAddress() {
        return address;
      }
      public void setAddress(Address address) {
        this.address = address;
      }
      @OneToMany
      public Collection getOrders() {
        return orders;
      }
      public void setOrders(Collection orders) {
        this.orders = orders;
      }
      @ManyToMany
      public Set getPhones() {
        return phones;
      }
      public void setPhones(Set phones) {
        this.phones = phones;
      }
      // Business method to add a phone number to the customer
      public void addPhone(PhoneNumber phone) {
        this.getPhones().add(phone);
        // Set the phone's ref to this customer
        phone.setCustomer(this);
      }
      }
    }
Oracle Chatbot
Disconnected