Important JPA Annotations with Examples

This post lists important and commonly used JPA annotations with sample code snippets.

Important JPA Annotations

Let us look at some of the important JPA annotations. Note that these annotations are present in javax.persistence package.

@Entity: Specifies that the class is an entity. This annotation can be applied to Class, Interface of Enums. Example:
public class Student {}

@Table: It specifies the table in the database with which this entity is mapped. In the example below the data will be stored in the “employee” table. The name attribute of @Table annotation is used to specify the table name.
@Table(name = "student")
public class Student {}

@Column: Specify the column mapping using @Column annotation. The name attribute of this annotation is used for specifying the table’s column name
    @Column(name = "first_name")
    private String firstName;

    @Column(name = "last_name")
    private String lastName;

This annotation specifies the primary key of the entity.
@Table(name = "student")
public class Student {

    private int id;

@GeneratedValue: This annotation specifies the generation strategies for the values of primary keys.
@Table(name = "student")
public class Student {

    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id")
    private int id;

Sort your data using @OrderBy annotation. In the example below, it will sort all Students by their id in ascending order.
    private String firstName;

@Transient: Every non-static and non-transient property of an entity is considered persistent unless you annotate it as @Transient.
    private String firstName;

@Lob: Large objects are declared with @Lob.
    private byte[] data;

@OneToOne: Used to define the one-to-one mapping between two entity beans.
@Table(name = "instructor")
public class Instructor {

    @OneToOne(cascade = CascadeType.ALL)
    @JoinColumn(name = "instructor_detail_id")
    private InstructorDetail instructorDetail;


@OneToMany: The @OneToMany JPA annotation is used to link one-to-many unidirectional entity mapping.
    @OneToMany(cascade = CascadeType.ALL)
    private List < Course > courses = new ArrayList < Course > ();

@ManyToOne: This annotation specifies a single-valued association to another entity class that has many-to-one multiplicity.
    @ManyToOne(cascade = CascadeType.ALL)
    @JoinColumn(name = "instructor_id")
    private Instructor instructor;
@ManyToMany: In many-to-many association, the source entity has a field that stores a collection of target entities. The @ManyToMany JPA annotation is used to link the source entity with the target entity.

Consider the following tables where employees and projects exhibit a many-to-many relationship between each other -
The many-to-many relationship is implemented using a third table called employees_projects which contains the details of the employees and their associated projects. Note that here Employee is a primary entity.
    @ManyToMany(cascade = {
        name = "employees_projects",
        joinColumns = {
            @JoinColumn(name = "employee_id")
        inverseJoinColumns = {
            @JoinColumn(name = "project_id")
    Set < Project > projects = new HashSet < Project > ();

All JPA Annotations

Free Spring Boot Tutorial - 5 Hours Full Course

Watch this course on YouTube at Spring Boot Tutorial | Fee 5 Hours Full Course