The Java Persistance API (JPA) provides the capability of object relational mapping for managing relational data in Java applications.
The Java Persistance API provides features for mapping relational data to Java objects or entities, managing the entities, querying the entities and also in creating the database schema.
An Entity class is a lightweight persistence class, that is typically mapped to a relational table. An Entity class is decorated with the annotation javax.persistence.Entity. Each Entity class instance corresponds to a row in a relational table.
Fields or properties in an entity class are mapped to the relational data by decorating them with annotations.
An Entity class must follow the following requirements.
@Entity
public class Order { ... }
Persistent fields in an Entity class which are of type Collection are annotated with the javax.persistence.ElementCollection annotation.
@Entity
public class Order {
@ElementCollection
protected ListorderLines = new ArrayList<>();
}
The javax.persistence.ElementCollection has an attribute fetch which can be specified with two values either 'LAZY' or 'EAGER'
//Load order lines eagerly
@Entity
public class Order {
@ElementCollection(fetch=EAGER)
protected ListorderLines = new ArrayList<>();
}
You can define a field as a primary key in an Entity class by annotating the field with javax.persistence.id annotation.
@Entity
public class Order {
@id
protected String orderId
}
Composite primary key consists of more than one attribute, which corresponds to a set of single persistent properties or fields.
Composite primary keys must be defined in a primary key class.
Composite primary keys are annotated using the javax.persistence.EmbeddedId and javax.persistence.IdClass annotations.
There are four multiplicity relationships between JPA Entity classes.
1. One-to-one - Each instance of an entity is related to a single instance of another entity. One-to-one relationships use the javax.persistence.OneToOne annotation on the corresponding persistent property or field.
2. One-to-many - Each instance of an entity is related to multiple instances of another entity. One-to-many relationships use the javax.persistence.OneToMany annotation on the corresponding persistent property or field.
3. Many-to-one - Multiple instances of an entity are related to a single instance of another entity. Many-to-one relationships use the javax.persistence.ManyToOne annotation on the corresponding persistent property or field.
4. Many-to-Many - Multiple instances of an entity are related to multiple instances of another entity. Many-to-many relationships use the javax.persistence.ManyToMany annotation on the corresponding persistent property or field.
There are two kinds of directional relationships between JPA entity classes.
Bidirectional - In a bidirectional relation ship, each entity has a field or property that refers to the other entity.
Unidirectional - In a unidirectional entity, only one entity has a field or property that refers to the pother entity.
Cascade operations defines the dependency of an entity object on the existence of another entity object, where there is a dependency between the two objects.
There are six different types of cascade operations.
1. DETACH - Is parent entity is detached from the persistence context, then the related entity will also be detached.
2. MERGE - If the parent entity is merged into the persistence context, then the related entity will also be merged.
3. PERSIST - If the parent entity is persisted into the persistence context, then the related entity will also be persisted.
4. REFRESH - If the parent entity is refreshed in the persistence context, then the related entity will also be refreshed.
5. REMOVE - If the parent entity is removed from the persistence context then the related entity will also be removed.
6. ALL - All the cascade operations will be applied to the parent entity's related entity.
@OneToMany(cascade=REMOVE)
public ListgetOrdersLines() { return ordersLines; }
Embedded classes represent part of the state of an entity, but do not have a persistent identity of their own. Entity classes are annotated with the javax.persistence.Embeddable annotation.
@Entity
public class Person {
@Id
protected long id
protected String fname;
protected String lname;
@Embedded
protected Address address;
...
}