Entity Mapping is the process of mapping Java classes and their fields to relational database tables and columns using JPA annotations. A Java class is marked as an entity using @Entity, while @Table can be used to specify the database table name. The @Id annotation identifies the primary key, and @GeneratedValue can be used to generate primary-key values automatically. Fields can be mapped to specific database columns using @Column. Entity mapping allows Hibernate to automatically translate Java objects into database records and database records back into Java objects, reducing the need to write SQL and manual mapping code.
Features of Entity Mapping
- Class-to-Table Mapping – Maps a Java class to a database table using
@Entityand@Table. - Field-to-Column Mapping – Maps Java fields to database columns using
@Column. - Primary Key Mapping – Uses
@Idto identify the primary key. - Automatic ID Generation – Uses
@GeneratedValueto generate IDs automatically. - Custom Table Names –
@Tableallows developers to specify a custom table name. - Custom Column Names –
@Columnallows fields to be mapped to specific column names. - Column Constraints – Supports constraints such as
nullable,unique, andlength. - ORM Support – Hibernate automatically converts Java objects into relational records.
- Database Independence – Entity classes can be used with different relational databases.
- Reduced SQL Code – Basic persistence operations can be performed without manually writing SQL.
