Hibernate Mapping Entities

Hibernate mapping refers to the process of defining how the Java objects in an application map to the tables and columns in a relational database. Hibernate uses a variety of annotations and XML configuration files to define these mappings. The most common annotations used for mapping are:

  • @Entity: Indicates that a class is an entity and should be persisted to the database.

  • @Id: Indicates the primary key of an entity.

  • @GeneratedValue: Indicates that the primary key should be generated automatically.

  • @Column: Specifies the mapping of a property to a specific column in the database table.

  • @OneToOne: Indicates a one-to-one relationship between entities.

  • @OneToMany: Indicates a one-to-many relationship between entities.

  • @ManyToOne: Indicates a many-to-one relationship between entities.

  • @ManyToMany: Indicates a many-to-many relationship between entities.

Here is an example of how to map a simple entity using Hibernate annotations:

    @Entity
    public class Employee {
        @Id
        @GeneratedValue
        private int id;
        @Column(name = "full_name")
        private String name;
        private String department;

        // getters and setters
    }
        

In this example, the Employee class is annotated with @Entity to indicate that it's an entity that should be persisted to the database. The id property is annotated with @Id and @GeneratedValue to indicate that it's the primary key and should be generated automatically.