The domain model has a class. The database has a table. JPA is a simple way to convert one to the other automatically.
The technique of bridging the gap between the object model and the relational model is known as object-relational mapping, or O-R mapping or simply ORM.
Regular Java classes can be transformed into entities by annotating them.
Let's start by creating a regular Java class for an employee.
public class Employee { private int id; private String name; //from w ww . j a v a2 s. c om public Employee() { } public Employee(int id) { this.id = id; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } }
This class resembles a JavaBean-style class with two properties: id and name.
Each of these properties is represented by a pair of accessor methods to get and set the property, and is backed by a member field.
To turn Employee into an entity,
we first annotate the class with @Entity
.
It is a marker annotation to indicate to the persistence
engine that the class is an entity.
Then we use @Id
annotation to mark a field as the primary key.
The following code shows the entity class.
@Entity public class Employee { @Id private int id; private String name; public Employee() { } public Employee(int id) { this.id = id; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } }