The creation of a simple entity is not difficult and is accomplished using the @Entity annotation. The essence of the process is demonstrated here and elaborated upon in subsequent recipes.
The creation of an entity involves two steps:
An entity is frequently added using a wizard provided by an IDE. This frequently involves specifying a primary key and the creation of a persistence unit. The primary key is used to optimize access to the underlying database table and the persistence unit provides mapping from the entity to the underlying data store.
Create a Java EE application called EntityBeanApplication
. In the EJB module, add a package called packt
with an entity called PartsBean
. While we will use the EJB module, EJB components do not have to be packaged in the EJB jar but can now be packaged as part of the WAR file in EJB 3.1.
The PartsBean
is intended to represent a part. The initial version of the class has fields for:
The first step is to declare the class an entity using the @Entity annotation. If an entity is passed by value then it must implement the Serializable
interface. We implement the interface here in case we need to pass the entity later.
@Entity public class PartsBean implements Serializable { @Id @GeneratedValue(strategy = GenerationType.AUTO) private Long id; public Long getId() { return id; } private String name; private int partNumber; private float weight; private int quantity; public String getName() { return name; } public void setName(String name) { this.name = name; } public int getPartNumber() { return partNumber; } public void setPartNumber(int partNumber) { this.partNumber = partNumber; } public int getQuantity() { return quantity; } public void setQuantity(int quantity) { this.quantity = quantity; } public float getWeight() { return weight; } public void setWeight(float weight) { this.weight = weight; } }
Each entity has a unique key used to identify it. For the PartsBean
we used a long value called id
. The @Id annotation identified the field as a primary key. The use of the @GeneratedValue field with the strategy of GeneratationType.AUTO, means the key will be generated automatically. A get method was provided for the field. We cannot change the primary key so we did not provide a set method for it.
Since the id
field was auto-generated, when an entity is created the value assigned may differ from what is shown in the examples in this chapter. The number provided depends on the actual generator and the number of entities of that type created.
The class developed so far represents a minimal entity. However, there are other methods proven to be useful for an entity. Two commonly provided methods include: equals
and toString
. Both of these methods override the base class object.
The equals
method is used to compare the equality of two entities. It first determines whether the object it is being compared to is an actual PartsBean
. If so, it verifies that the id
fields are the same.
@Override public boolean equals(Object object) { if (!(object instanceof PartsBean)) { return false; } PartsBean other = (PartsBean) object; if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) { return false; } return true; }
The implementation of this toString
method returns a String containing the id
of the PartsBean
.
@Override public String toString() { return "packt.PartsBean[id=" + id + "]"; }
18.216.27.251