The basic unit of persistence in JPA is the entity, which is nothing more than a regular Java class with metadata to describe how its state maps to the database tables. Metadata may be in the form of annotations on the entity class itself, or it may be an accompanying XML file, but we are using annotations since they are easier to specify and understand.
When used together, XML mappings can override the values specified in annotations
Every entity class should have an @Entity
marker and an identifier field, indicated by @Id
, that is mapped to the primary key column in the database. When a field contains simple data and maps to a regular column in the database we call it a basic mapping, thus an identifier field is a special kind of basic mapping. When an entity has a field that references one or more other entities, that field maps to a foreign key column, and is called a relationship field. Other than the identifier field, basic mappings do not need to be annotated, but relationships must be specified by their relationship cardinality.
Defaulting rules in JPA mean that you are not required to specify table names and column names that an entity is mapped to. If you are not happy with the JPA-assigned defaults then you can always override them through the use of additional mapping metadata. For example, by putting @Table on the entity class you can make the table name explicit, and by annotating a basic mapping field with @Column you can define the particular column that maps the state in that field. Likewise @JoinColumn is used to override the name of the foreign key column for relationship references.
An example of two mapped entities are the Pet and Owner classes shown in Listings 1 and 2.
Listing 1 - Pet entity class
@Entity
@Table(name="PET_INFO")
public class Pet {
@Id
@Column(name="ID")
int licenseNumber;
String name;
PetType type;
@ManyToOne
@JoinColumn(name="OWNER_ID")
Owner owner;
...
}
Listing 2 - Owner entity class
@Entity
public class Owner {
@Id
int id;
String name;
@Column(name="PHONE_NUM")
String phoneNumber;
@OneToOne
Address address;
@OneToMany(mappedBy="owner")
List<Pet> pets;
...
}
In a bidirectional relationship pair, such as the @OneToMany relationship in Owner
to Pet
and the @ManyToOne
relationship back from Pet
to Owner
, only one foreign key is required in one of the tables to manage both sides of the relationship. As a general rule, the side that does not have the foreign key in it specifies a mappedBy
attribute in the relationship annotation and specifies the field in the related entity that maps the foreign key.
The possible mapping annotations that can be used are:
@Basic @Enumerated @ManyToOne @Temporal
@Embedded @Lob @OneToMany @Transient
@EmbeddedId @ManyToMany @OneToOne
|
@AttributeOverride(s) @PrimaryKeyJoinColumn(s)
@AssociationOverride(s) @SecondaryTable(s)
@Column @SequenceGenerator
@DiscriminatorColumn @Table
@JoinColumn(s) @TableGenerator
@JoinTable
|
@Entity @IdClass @MappedSuperclass
@Embeddable @Inheritance @OrderBy
@GeneratedValue @DiscriminatorValue @Version
@Id @MapKey
|
{{ parent.title || parent.header.title}}
{{ parent.tldr }}
{{ parent.linkDescription }}
{{ parent.urlSource.name }}