Articles
Enterprise Architecture
An Introduction to the Enterprise JavaBeans 3.0 (EJB 3) Specification
Pages:
1,
2,
3,
4,
5,
6
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.
Entity beans do not need home interfaces.
Entity beans do not need business interfaces. They are optional.
For single-valued persistent properties, the method signatures are:
T getProperty()
void setProperty(T t)
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);
}
}
}