Top 15 JPA Annotations in Spring Boot (With Easy Examples)

    Top 15 JPA Annotations in Spring Boot (With Easy Examples)

    Discover the 15 most essential JPA annotations in Spring Boot with beginner-friendly explanations and real-world examples. Learn how to map Java classes to database tables, define relationships, and simplify persistence without writing SQL.

    default profile

    Munaf Badarpura

    July 05, 2025

    9 min read

    JPA stands for Java Persistence API. it is a specification in Java that provides a standard way to map Java objects or entities 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, you will learn the top 15 essential JPA annotations that make it easy to map Java classes to database tables, define relationships between entities, and manage data persistence without writing raw SQL.

    So if you are building any project that involves database interactions mastering these 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.

    Example:

    @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.

    11. @JoinColumn#

    The @JoinColumn annotation is used to specify the foreign key column in relationships like @ManyToOne, @OneToOne, or even in @OneToMany. Without this annotation, JPA will create the join column with a default name, but with @JoinColumn you can customize the column name and its constraints.

    Example:

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

    Here, @JoinColumn(name = "user_id") tells JPA to use a column named user_id in the Order table to link to the User entity. The nullable = false ensures every order must be linked to a user.

    12. @JoinTable#

    The @JoinTable annotation is used with @ManyToMany relationships to define the join (linking) table and its join columns. By default, JPA generates a join table for @ManyToMany, but this annotation lets you customize its name and structure.

    Example:

    @Entity @Data public class Teacher { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; private String name; @ManyToMany @JoinTable( name = "teacher_subject", joinColumns = @JoinColumn(name = "teacher_id"), inverseJoinColumns = @JoinColumn(name = "subject_id") ) private List<Subject> subjects; }

    In this example, a join table named teacher_subject is created with two foreign key columns: teacher_id and subject_id, which connect the two entities.

    13. @Embedded#

    The @Embedded annotation is used when you want to include reusable value-type components (non-entity objects) inside your entity class. These embedded objects don’t have a separate table — their fields are stored as part of the entity’s table.

    Example:

    @Embeddable @Data public class Address { private String street; private String city; private String zip; } @Entity @Data public class Customer { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; private String name; @Embedded private Address address; }

    Here, the Address class is embedded inside the Customer table — all its fields (street, city, zip) will become columns in the Customer table.

    14. @Embeddable#

    The @Embeddable annotation is used on the class that is going to be embedded using @Embedded. It marks that the class is not a full entity but a value object that can be part of other entities.

    This annotation works hand-in-hand with @Embedded.

    Example:

    @Embeddable @Data public class Address { private String street; private String city; private String zip; }

    In this case, the Address class is treated as a reusable component that can be used in multiple entities (e.g., Customer, Employee, etc.) without creating a separate table.

    15. @Lob#

    The @Lob annotation is used when you want to store large objects like text, images, or files in the database. It stands for Large Object, and it helps JPA decide whether to store a field as a BLOB (Binary Large Object) or CLOB (Character Large Object) depending on the field type.

    Example:

    @Entity @Data public class Document { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; private String title; @Lob private String content; // Large text content @Lob private byte[] fileData; // Binary file (e.g., PDF, image) }

    Here, content will be stored as CLOB and fileData as BLOB. This is especially useful for documents, images, or file upload scenarios.

    Conclusion#

    I hope you now have a clear understanding of each JPA annotation covered in this article. These annotations make it easy to map Java classes to database tables, define relationships like one-to-many or many-to-one, and manage data persistence with minimal effort.

    Want to Master Spring Boot and Land Your Dream Job?

    Struggling with coding interviews? Learn Data Structures & Algorithms (DSA) with our expert-led course. Build strong problem-solving skills, write optimized code, and crack top tech interviews with ease

    Learn more
    Spring Boot
    Spring
    JPA
    Annotations
    Java Persistence API

    Subscribe to our newsletter

    Read articles from Coding Shuttle directly inside your inbox. Subscribe to the newsletter, and don't miss out.

    More articles