← Back to Questions
Spring Boot

What is @Table annotation?

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

What is @Table Annotation in JPA?

@Table is a JPA annotation used to specify the database table details for an entity class.

It tells JPA and Hibernate which database table should be mapped to the entity class.

In simple words:

“@Table connects a Java entity class to a specific database table.”

What Does JPA Mean?

JPA stands for:

Java Persistence API

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


Why @Table Annotation is Needed

By default, Hibernate uses the entity class name as the table name.

Sometimes:

  • Database table names are different
  • Table names follow naming conventions
  • Multiple schemas are used
  • Custom table mapping is required

In such cases, @Table is used.


Basic @Table Example

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

    @Id
    private Long id;

    private String name;

    private String email;
}

What Happens Internally?

Hibernate reads:

@Table(name = "students")

and maps:

  • Student class → students table

How Mapping Works

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

What Happens Without @Table?

If @Table is not specified, Hibernate uses the class name as the table name automatically.

Example:

@Entity
public class Student {
}

Hibernate may create:

student

table by default.


Why Explicit @Table is Recommended

Explicit table mapping improves:

  • Readability
  • Maintainability
  • Database consistency

Syntax of @Table Annotation

@Table(
    name = "table_name",
    schema = "schema_name",
    catalog = "catalog_name"
)

Main Attributes of @Table

Attribute Purpose
name Specifies table name
schema Specifies schema name
catalog Specifies catalog name
uniqueConstraints Defines unique constraints

Schema Example

@Entity
@Table(
    name = "students",
    schema = "college_db"
)
public class Student {
}

Unique Constraint Example

@Entity
@Table(
    name = "students",
    uniqueConstraints = {
        @UniqueConstraint(
            columnNames = "email"
        )
    }
)
public class Student {
}

Generated SQL Example

CREATE TABLE students (

    id BIGINT PRIMARY KEY,

    email VARCHAR(255) UNIQUE

)

@Entity and @Table Together

Usually both annotations are used together.


Example

@Entity
@Table(name = "employees")
public class Employee {

    @Id
    private Long id;

    private String name;
}

What is @Column Annotation?

While @Table maps the class to a table, @Column maps fields to columns.


@Column Example

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

Difference Between @Entity and @Table

Feature @Entity @Table
Purpose Marks class as entity Defines table details
Mandatory Yes No
Used For Entity recognition Custom table mapping

How Hibernate Uses @Table

Hibernate reads:

  • Entity metadata
  • Table details
  • Schema information

and automatically generates SQL queries.


Save Operation Example

@Autowired
private EmployeeRepository repository;

public Employee saveEmployee() {

    Employee employee = new Employee();

    employee.setName("Naresh");

    return repository.save(employee);
}

Generated SQL

INSERT INTO employees(name)
VALUES (?)

Best Practices for @Table

  • Always use meaningful table names
  • Use lowercase table names consistently
  • Use plural names for collections
  • Define unique constraints properly
  • Keep naming conventions consistent

Advantages of @Table Annotation

  • Custom table mapping
  • Supports schemas and catalogs
  • Improves readability
  • Supports unique constraints
  • Better database organization

Disadvantages of Improper Table Mapping

  • Incorrect table names may cause runtime errors
  • Poor naming conventions reduce maintainability
  • Schema mismatches may break applications

Real-Time Example in E-Commerce Application

E-commerce application contains:

  • products table
  • orders table
  • customers table

Each entity uses:

@Table(name = "products")

to map Java classes to database tables properly.


Using @Table with Relationships

Entities with relationships still use:

@Table

for table mapping.


Relationship Example

@Entity
@Table(name = "orders")
public class Order {

    @Id
    private Long id;

    @ManyToOne
    private Customer customer;
}

Common Interview Questions on @Table

What is @Table annotation?

@Table specifies the database table details for an entity class.

Is @Table mandatory?

No, Hibernate uses the class name as the default table name.

What is the purpose of name attribute in @Table?

It specifies the table name explicitly.

What is the difference between @Entity and @Table?

@Entity marks the class as entity, while @Table defines table mapping details.

Can @Table define unique constraints?

Yes, using uniqueConstraints attribute.


Conclusion

The @Table annotation is an important JPA annotation used for mapping entity classes to database tables.

It provides:

  • Custom table mapping
  • Schema support
  • Unique constraints
  • Better database organization

@Table helps developers maintain clean, scalable, and maintainable database structures in Spring Boot and Hibernate applications.

Understanding @Table is essential for Spring Boot developers because proper database mapping is critical in enterprise application development.

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.