Mapping Java entities to database tables

One of the key aspects of working with Hibernate and JPA is mapping Java entities to database tables. This mapping allows us to seamlessly interact with the underlying database while working with object-oriented entities in our Java application.

Entity classes

In order to perform the mapping, we first need to create Java classes that represent our database tables. These classes are commonly referred to as entity classes. Each entity class represents a specific table in the database and contains attributes that correspond to the table's columns.

For example, let's consider a simple database with a "Customer" table that has columns like "id", "name", and "email". In our Java application, we would create a Customer entity class with attributes for each of these columns.

@Entity
@Table(name = "Customer")
public class Customer {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    private String name;

    private String email;

    // getters and setters
}

In this example, the @Entity annotation signifies that this class is an entity and should be mapped to a database table. The @Table annotation provides the mapping information for the table name.

Mapping attributes

Once we have our entity classes set up, we need to map each attribute of the entity to its corresponding column in the table. This is achieved using the various annotations provided by Hibernate and JPA.

For example, to map the "name" attribute of the Customer entity to the "name" column in the table, we can use the @Column annotation:

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

We can specify additional mapping information within the @Column annotation, such as the column name, length, nullable, and more.

Relationship mapping

In addition to mapping attributes to columns, we often need to establish relationships between entities. This can be achieved using annotations like @OneToOne, @OneToMany, @ManyToOne, and @ManyToMany.

For instance, let's say we have a "Product" entity class and each customer can have multiple products. We can establish a one-to-many relationship between Customer and Product using the @OneToMany annotation:

@OneToMany(mappedBy = "customer")
private Set<Product> products;

In this example, the mappedBy attribute specifies the inverse side of the relationship (the "customer" attribute in the Product entity class).

Conclusion

Mapping Java entities to database tables is a fundamental step when working with Hibernate and JPA. It allows us to seamlessly integrate our Java application with a relational database, simplifying data manipulation and retrieval.

In this article, we discussed how to create entity classes representing database tables, map attributes to columns using annotations like @Column, and establish relationships between entities using annotations like @OneToMany. These concepts form the foundation of effective database mapping in Hibernate and JPA.


noob to master © copyleft