JPA provides two annotations @Table and @Entity to map Java classes to the database table. These annotations are used together in the classes but the difference between @Table and @Entity in JPA, their purposes and functionalities.
This topic will provide a detailed comparison between @Entity and @Table, their use cases and examples to help us use them effectively.
What is the @Entity in JPA?
This @Entity annotation is a JPA annotation that makes a Java class an entity, which means it is mapped to a database table and used to enable JPA functionalities like persistence, retrieval, defining query and query execution.
Key Features of @Entity
- It marks a class as a persistent entity.
- It maps the class to a database table (default table name is the class name).
- It validates the entity configuration at application startup.
- It serves as the foundation for ORM functionality.
Example: Using @Entity
import jakarta.persistence.Entity;
import jakarta.persistence.Id;
@Entity
public class Product {
@Id
private Long id;
private String name;
private double price;
// Getters and Setters
}
Explanation
- @Entity marks the Product class as an entity.
- JPA will automatically map the class to a table named Product unless overridden by @Table.
What is @Table in JPA?
The @Table annotation is a JPA annotation used to customize the mapping of an entity to a specific database table like specifying the table name, schema, catalog, and unique constraints.
Key Features of @Table
- It specifies the name of the database table.
- It allows setting the schema and catalog for the table.
- It supports defining unique constraints at the table level.
- Optional if the default table name (class name) is acceptable.
Example: Using @Table
import jakarta.persistence.Entity;
import jakarta.persistence.Id;
import jakarta.persistence.Table;
@Entity
@Table(name = "products", schema = "inventory")
public class Product {
@Id
private Long id;
private String name;
private double price;
// Getters and Setters
}
Explanation
- @Table(name = “products”) specifies that the Product entity is mapped to the products table.
- schema = “inventory” maps the table to the inventory schema.
Key Difference Between @Table and @Entity
Feature | @Entity | @Table |
Purpose | It declares a class as a JPA entity. | It customizes the table mapping for the entity. |
Usage | It is mandatory for all JPA entity classes. | It is optional unless custom table mapping is needed. |
Default Behavior | It maps the class name to a table with the same name. | No effect unless explicitly used (relies on @Entity). |
Attributes | None related to table mapping. | Allows specifying the table name, schema, catalog, and constraints. |
Interdependence | It can exist without @Table. | It requires @Entity to be meaningful. |
Customization | It does not allow table customization. | It provides flexibility for table customization. |
How @Entity and @Table Work Together
- @Entity is mandatory for defining a persistent class.
- @Table is optional and used for customizing the table mapping.
- When both are used, @Table overrides the default table name derived from the class name.
Example: Combined Usage
import jakarta.persistence.Entity;
import jakarta.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name = "product_details", schema = "store")
public class Product {
@Id
private Long id;
private String name;
private double price;
// Getters and Setters
}
- @Entity: Marks Product as an entity.
- @Table(name = “product_details”): Maps the entity to the product_details table in the store schema.
When to Use @Table
- Custom Table Names: When the table name differs from the class name.
- Schema Specification: When mapping the entity to a specific schema or catalog.
- Unique Constraints: To define unique constraints at the table level.
When to Use @Entity Without @Table
If @Table is not specified, the entity’s class name is used as the table name by default.
Example:
import jakarta.persistence.Entity;
@Entity
public class Product {
@Id
private Long id;
private String name;
private double price;
}
- Table name defaults to Product.
Practical Comparison
Scenario | @Entity | @Table |
Declaring a class as an entity | Mandatory for all database entities. | Not applicable alone; used with @Entity. |
Changing the table name | Not supported. | Use @Table(name = “custom_name”). |
Specifying schema or catalog | Not supported. | Use @Table(schema = “schema_name”). |
Enforcing unique constraints | Not supported directly. | Use @Table(uniqueConstraints = …). |
Best Practices
- Always Use @Entity: Every JPA entity class must include @Entity.
- Use @Table for Customization: Use @Table only when you need to customize table mapping.
- Follow Naming Conventions: Align table and column names with database standards for consistency.
- Keep It Simple: Avoid unnecessary complexity by using defaults unless customization is required.
Conclusion
@Entity and @Table are both essential in JPA for defining and customizing entity-to-table mappings:
- @Entity is mandatory for declaring a class as an entity.
- @Table is optional and used to customise the table name, schema, or constraints.
By understanding their differences and using them appropriately, you can create efficient and maintainable ORM mappings in our Spring Boot applications.