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:
@Entity
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.
@Entity
@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;

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

    @Id
    private int id;
}

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

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

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

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

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

@OneToOne: Used to define the one-to-one mapping between two entity beans.
@Entity
@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 = {
        CascadeType.ALL
    })
    @JoinTable(
        name = "employees_projects",
        joinColumns = {
            @JoinColumn(name = "employee_id")
        },
        inverseJoinColumns = {
            @JoinColumn(name = "project_id")
        }
    )
    Set < Project > projects = new HashSet < Project > ();

All JPA Annotations




Comments