1. Entity and Table Mappings
1. @Entity
About
@Entityis a JPA (Java Persistence API) annotation.It marks a plain Java class as an entity bean, which means this class will be mapped to a relational database table.
When the persistence provider (like Hibernate) sees a class annotated with
@Entity, it treats the class as a persistent entity, capable of being managed, saved, deleted, and queried from the database.
Important Points
Mandatory for persistence: Only classes annotated with
@Entityare recognized as entities by the JPA provider.No-argument constructor required: The class must have a public or protected no-argument constructor (this is needed by JPA at runtime via reflection).
Serializable: Although not mandatory by specification, it is recommended that entity classes implement
Serializablefor best practices, especially in distributed systems.Uniquely identifiable: Every entity must have a primary key field, which is annotated with
@Id.Class cannot be final: The JPA provider may need to subclass or proxy the entity.
Fields/Properties: The class can either have fields or getter/setter methods that are persisted, depending on whether you use field access or property access (determined by where the
@Idis placed).
Syntax and Usage
import jakarta.persistence.Entity;
@Entity
public class Employee {
@Id
private Long id;
private String name;
private String department;
}Here:
Employeeis now a managed entity.JPA will create a table for it or map it to an existing table.
Rules for Using @Entity
@EntityEntity name
If no name is specified, the class name is used as the entity name.
Table name
If no @Table annotation is present, the table name defaults to the entity name (case-sensitive, depending on database).
Must not be final
JPA providers use proxies; final classes cannot be proxied easily.
Must have a primary key
Without an @Id, the entity cannot be managed.
No-arg constructor
Required by the persistence provider.
Optional Attributes of @Entity
@Entityname: Defines the name of the entity (used in JPQL queries). Defaults to the class name if not provided.
Note: The name has nothing to do with the table name in the database (that's handled by
@Table).
2. @Table
About
@Tableis a JPA annotation used to customize the database table that an entity is mapped to.By default, if
@Tableis not specified, JPA uses the class name as the table name.@Tableallows developers to control the table name, schema, catalog, and indexes.
Important Points
Customization of mapping:
@Tabletells JPA what actual database table should back the entity.Optional: It is not mandatory. Without
@Table, the entity will map to a table named exactly like the entity class.Defines schema/catalog: It can specify the database schema or catalog in multi-schema setups.
Define unique constraints: You can define database-level unique constraints across one or more columns.
Syntax and Usage
Here:
employeesis the actual table name in thecompanyschema of thecorporatecatalog.The
Employeeentity will persist records intocorporate.company.employees.
Attributes of @Table
@Tablename
The name of the database table.
@Table(name = "employees")
schema
Name of the database schema.
@Table(schema = "company")
catalog
Name of the database catalog.
@Table(catalog = "corporate")
uniqueConstraints
Defines unique constraints on one or more columns.
@Table(uniqueConstraints = @UniqueConstraint(columnNames = {"email"}))
indexes
Defines indexes on one or more columns (since JPA 2.1).
@Table(indexes = @Index(name = "idx_name", columnList = "name"))
Practical Details About Attributes
name
nameSpecifies the table's name in the database.
If omitted, the entity class name is used.
If different from the entity name, always specify
name.
schema
schemaUseful when your database is logically divided into schemas (especially common in Oracle, PostgreSQL).
If your table resides in a different schema, you must specify it.
catalog
catalogRefers to the database catalog.
Rarely used compared to
schema.In some RDBMS like MySQL, catalog and database are the same.
uniqueConstraints
uniqueConstraintsYou can define one or more columns that must have unique values.
It generates a UNIQUE constraint at the database level.
Means the email column must be unique for all rows.
indexes
indexesIntroduced in JPA 2.1.
Specifies index creation on columns.
Helps in performance optimization at the database level.
Creates an index named idx_employee_name on the name column.
Last updated