Top 10 JPA Annotations In Spring Boot

    Top 10 JPA Annotations In Spring Boot

    Learn the Top 10 Essential JPA Annotations for Spring Boot. Master how to map Java classes to database tables, manage relationships, and simplify database operations without writing SQL.

    default profile

    Munaf Badarpura

    July 05, 2025

    6 min read

    JPA stand for “Java Persistence API”, it is a specification in Java that provides a standard way to map Java objects to tables in database. It helps developers to perform database operations without writing complex SQL queries manually.

    Before JPA annotations, developers used to interact with database using raw JDBC code. and for this developers need to manually write SQL queries for all database operations. but JPA annotations make much easier when its time to interacting with databases.

    In this article, I have explained the top 10 essential JPA annotations, these annotations allow you to easily map your Java classes to database tables also if you want to establish relationships between tables you do not need to write SQL queries instead JPA provides powerful annotations that simplify this task

    So if you are building any project that involves database interactions mastering this annotations is like necessary.

    1. @Entity#

    The @Entity annotations is most basic annotation in JPA. this annotation is used to mark a Java class as a database entity. that means the class will be mapped to a table in your database. each field in your entity class represents a column in the database table.

    EXAMPLE :

    @Entity @Data public class Product { @Id private Long id; private String name; private double price; }

    2. @Table#

    The @Table annotations is used to provide additional information about table in database. So as i mentioned before the @Entity annotation marks a class as a database entity but the @Table annotation is used to customize the table's name, schema, and unique constraints etc.

    EXAMLE :

    @Entity @Table(name = "products") @Data public class Product { @Id private Long id; private String name; private double price; }

    In this example the Java class is Product but the table in the database will be created as “products” because of the @Table(name = "products") annotation.

    3. @Id#

    In the previous examples you have already seen this @Id annotation inside Product class, so now lets understand this annotation. The @Id annotation is used to specify the primary key of an entity it means field that is annotated with @Id annotation acts as a primary key column in database.
    EXAMPLE :

    @Entity @Data public class Product { @Id private Long id; private String name; private double price; }

    here the field marked with @Id is treated as primary key in the your database Product table.

    4. @GeneratedValue#

    The @GeneratedValue annotation is used along with @Id annotation so it automatically generate the primary key value for new records, and that is why you do not need to manually assign primary key value for each entry. If you don’t use @GeneratedValue annotation, you must manually set the value of the primary key every time you create a new entity.

    Also we specify strategies in @GeneratedValue to define how the value should be assign.

    Here is the most common GenerationType Strategies

    1. GenerationType.IDENTITY : Database auto-increments the ID (most common with MySQL).
    2. GenerationType.SEQUENCE : Uses database sequences (common in Oracle, PostgreSQL).
    3. GenerationType.AUTO : JPA picks the strategy based on the database.

    EXAMPLE :

    @Entity @Data public class Product { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; private String name; }

    Here in product table you do not need to specify the id value each time when you are saving entity because JPA automatically handles the id generation using auto-increment.

    5. @Column#

    The @Column annotation is used to map entity class field with column in a database table but JPA automatically maps fields to columns by default but this annotation is allows you to specify attributes of the column.

    EXAMPLE :

    @Entity @Data public class Product { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; @Column(name = "product_name", nullable = false, length = 100) private String name; @Column(nullable = false) private double price; }

    Here is the explanation

    → name = "product_name", maps name field to product_name in database table column.

    → nullable = false, specify that the column cannot be NULL.

    → length = 100, specify that maximum length of the column is 100 characters.

    6. @Transient#

    The @Transient annotation is used to mark entity field that this field should not be persisted to the table column. So if any field marked with @Transient will be ignored by JPA when performing database operations save(), update(), or fetching records. we use these kind of fields that is needed in business logic, but should not exists in the database table. 
    EXAMPLE :

    @Entity @Data public class Product { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; private double price; private double tax; @Transient private double finalPrice; }

    In this entity “finalPrice” field completely ignore during table creation and any database operations.

    7. @OneToOne#

    The @OneToOne annotation is used to define a one-to-one relationship between two entities, in simple terms one record in Table A is directly linked to one and only one record in Table B

    EXAMPLE :

    @Entity @Data public class User { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; private String name; @OneToOne private Passport passport; }

    Here the User entity has a reference to the Passport entity and JPA will automatically manage this relationship because of @OneToOne annotation.

    8. @OneToMany#

    The @OneToMany annotation is used to define a one-to-many relationship between two entities , in simple terms one record in Table A is related to multiple records in Table B. This is a very common relationship in real-world applications.
    EXAMPLE :

    @Entity @Data public class User { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; private String name; @OneToMany(mappedBy = "user") private List<Order> orders; }

    Here the User entity has a list of Order entities, we indicate that one id in the user class can have many orders from other entity called Order.

    9. @ManyToOne#

    The @ManyToOne annotation is used to define a many-to-one relationship between two entities, in simple terms many records in Table A are related to one record in Table B. This is the reverse side of a @OneToMany relationship.
    EXAMPLE :

    @Entity @Data public class Order { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; private String product; @ManyToOne @JoinColumn(name = "user_id") private User user; }

    Here the Order entity has a user field with @ManyToOne annotation it means many Orders can be linked to one User.

    10. @ManyToMany#

    The @ManyToMany annotation is used to define a many-to-many relationship between two entities, in simple terms many records in Table A are related to many records in Table B. in this type of relationship JPA creates a new mapping table to store references.
    EXAMPLE :

    @Entity @Data public class Student { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; private String name; @ManyToMany @JoinTable( name = "student_course", joinColumns = @JoinColumn(name = "student_id"), inverseJoinColumns = @JoinColumn(name = "course_id") ) private List<Course> courses; }

    Here the Student entity has a list of Course entities and @JoinTable annotation creates a join table named student_course to manage the many-to-many relationship. this join table has two columns: student_id and course_id.

    Conclusion#

    In this article, we have learned the top 10 essential JPA annotations that every Spring Boot developer should know. These annotations simplify database interactions so you do not need to write complex SQL queries manually to perform database operations.

    Spring Boot
    Spring
    JPA
    Annotations
    Java Persistence API

    More articles