← Back to Questions
Spring Boot

What is @Entity annotation?

Learn What is @Entity annotation? with simple explanations, real-time examples, interview tips and practical use cases.

What is @Entity Annotation in JPA?

@Entity is a JPA annotation used to mark a Java class as a database entity.

It tells JPA and Hibernate that the class should be mapped to a relational database table.

In simple words:

β€œThis Java class represents a table in the database.”

What Does JPA Mean?

JPA stands for:

Java Persistence API

JPA is used for Object Relational Mapping (ORM) between Java objects and relational database tables.


Why @Entity Annotation is Needed

Without @Entity:

  • JPA cannot recognize the class as a database entity
  • Hibernate will not map the class to a table
  • CRUD operations cannot be performed automatically

The @Entity annotation enables:

  • Automatic table mapping
  • Object persistence
  • CRUD operations
  • Relationship mapping

Simple Real-Time Example

Suppose a database contains:

students

table.

Then we create:

Student.java

entity class and mark it using:

@Entity

Basic @Entity Example

@Entity
public class Student {

    @Id
    private Long id;

    private String name;

    private String email;
}

What Happens Internally?

When Spring Boot starts:

  1. Hibernate scans entity classes
  2. @Entity classes are detected
  3. Entities are mapped to database tables
  4. SQL queries are generated automatically

How @Entity Maps Data

Java Class Database
Student Class students Table
id Field id Column
name Field name Column
email Field email Column

Important Requirements for Entity Classes

JPA entity classes should follow some rules.


Rules for Entity Classes

  • Class must be annotated with @Entity
  • Class must contain a primary key
  • Primary key should use @Id
  • Class should have default constructor
  • Class should not be final

Primary Key Requirement

Every entity must contain:

@Id

annotation to identify the primary key.


Primary Key Example

@Id
private Long id;

What is @Table Annotation?

@Table specifies the table name explicitly.


@Entity with @Table Example

@Entity
@Table(name = "students")
public class Student {

    @Id
    private Long id;

    private String name;
}

What Happens Without @Table?

If @Table is not provided, Hibernate uses the class name as the table name by default.

Example:

Student β†’ student

What is @Column Annotation?

@Column maps a field to a specific database column.


@Column Example

@Column(name = "student_email")
private String email;

Auto ID Generation Example

IDs can be generated automatically using:

@GeneratedValue

Example

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

Entity Lifecycle in JPA

Entity objects move through different states.


Entity Lifecycle States

State Description
Transient Object created but not saved
Persistent Object managed by Hibernate
Detached Object disconnected from session
Removed Object marked for deletion

Repository Example

public interface StudentRepository
        extends JpaRepository<Student, Long> {
}

Save Entity Example

@Autowired
private StudentRepository repository;

public Student saveStudent() {

    Student student = new Student();

    student.setName("Naresh");

    student.setEmail("naresh@gmail.com");

    return repository.save(student);
}

Generated SQL Example

Hibernate automatically generates:

INSERT INTO students(name, email)
VALUES (?, ?)

Entity Relationships

Entities support relationships between tables.


Relationship Types

Relationship Annotation
One-to-One @OneToOne
One-to-Many @OneToMany
Many-to-One @ManyToOne
Many-to-Many @ManyToMany

One-to-Many Example

@OneToMany(mappedBy = "student")
private List<Course> courses;

Advantages of @Entity Annotation

  • Automatic table mapping
  • Reduces boilerplate SQL code
  • Supports CRUD operations
  • Supports relationships
  • Simplifies database interaction

Disadvantages of Improper Entity Design

  • Poor performance
  • Complex object graphs
  • Lazy loading issues
  • Unnecessary memory usage

Common Mistakes with @Entity

  • Missing @Id annotation
  • Using final class
  • No default constructor
  • Improper relationship mapping
  • Excessive eager loading

Best Practices for Entity Classes

  • Keep entities simple
  • Always define primary key
  • Use meaningful table names
  • Use lazy loading carefully
  • Use DTOs for API responses when needed

Real-Time Example in E-Commerce Application

E-commerce application may contain entities such as:

  • Product
  • Order
  • Customer
  • Payment

Each entity class is mapped to a database table using the @Entity annotation.


Difference Between @Entity and @Table

Feature @Entity @Table
Purpose Marks class as entity Specifies table details
Mandatory Yes No
Used On Class Class

Common Interview Questions on @Entity

What is @Entity annotation?

@Entity marks a Java class as a JPA entity that maps to a database table.

Why is @Entity required?

Because Hibernate uses it to identify database entities.

Can an entity exist without @Id?

No, every entity must have a primary key.

What happens if @Table is not used?

Hibernate uses the class name as table name by default.

What is the difference between @Entity and DTO?

Entity maps database tables, while DTO transfers data between layers.


Conclusion

The @Entity annotation is one of the most important annotations in JPA and Hibernate.

It tells Hibernate to map Java classes to relational database tables automatically.

@Entity simplifies:

  • Database interaction
  • CRUD operations
  • Relationship mapping
  • Object persistence

Understanding @Entity is essential for Spring Boot developers because modern enterprise applications heavily rely on JPA and Hibernate for database management.

Why this Spring Boot question is important?

This interview question helps candidates understand real-time backend development concepts, practical problem solving, coding fundamentals, system design basics and production-ready application behavior.

Practice this question carefully for Java backend roles, Spring Boot developer interviews, microservices interviews, company interviews and full-stack developer preparation.

About the Author

Naresh Kumar is a Senior Java Backend Engineer with experience building enterprise applications using Java, Spring Boot, Microservices, Docker, Kubernetes and Cloud technologies.