Java Reference
In-Depth Information
ENTITIES
For the moment, the persistence unit declares one entity, fancyfoods.persistence
.FoodImpl . FoodImpl is an implementation of the Food interface we defined in sec-
tion 2.3.9. It allows Food objects to be persisted to, and read from, a database. The
nice thing about it, and the reason why so many people use JPA , is that it does all this
with one or two tiny little annotations; JPA does all the work for you, as shown in the
following listing.
Listing 3.3
The implementation of the FoodImpl entity class
@Entity(name = "FOOD")
public class FoodImpl implements Food {
Entity annotation declares JPA entity
@Id
private String name;
private double price;
private int quantity;
public FoodImpl() {
}
Primary key
No-argument
constructor for JPA
public FoodImpl(String name, double price, int quantity) {
this();
this.name = name;
this.price = price;
this.quantity = quantity;
With-argument
constructor for
convenience
}
@Override
public String getName() {
return this.name;
Getter and setter
for name column
}
public void setName(String name) {
this.name = name;
}
More getters
and setters
//...
Although it's too long to include in full here, the FoodImpl class is a thin data carrier.
It only contains getters and setters, without any business logic. It doesn't have respon-
sibility for persisting itself.
Enhancing the entities
Before they can be used by most JPA providers, entity POJO s must be enhanced .
Enhancement adds extra bytecode to the classes. If you're running in a Java EE 5 (or
later) application server, JPA entities will be automatically enhanced. Usefully, recent
versions of Apache Aries are also able to perform this enhancement. If you aren't run-
ning in a system that provides automatic enhancement, then an Ant task (or similar)
can be used to enhance the entities when building, but it can also be done using an
Eclipse builder.
 
Search WWH ::




Custom Search