Core Hibernate Annotations
Introduction
Hibernate is a powerful ORM (Object-Relational Mapping) framework that allows Java developers to map classes and their properties to database tables and columns, respectively. Hibernate annotations provide a clean and intuitive way to define these mappings directly in your Java code, making it easier to manage and maintain. This article will explore the key Hibernate annotations used for mapping entities, configuring relationships, and working with composite types.
Mapping Entities with Annotations
@Entity
The @Entity annotation is used to mark a Java class as an entity, meaning it will be mapped to a table
in the database. Every class annotated with @Entity must have a primary key, which is identified using
the @Id annotation.
1
2
3
4
5
6
7
8
9
10
11
12
import jakarta.persistence.Entity;
import jakarta.persistence.Id;
@Entity
public class User {
@Id
private Long id;
private String name;
// Getters and setters
}
In this example, the User class is marked as an entity, and its instances will be stored in a corresponding table
in the database.
@Table
The @Table annotation is used to specify the name of the database table that the entity maps to.
If not specified, Hibernate will assume the table name is the same as the class name.
1
2
3
4
5
6
7
8
9
10
11
12
import jakarta.persistence.Entity;
import jakarta.persistence.Table;
@Entity
@Table(name = "users")
public class User {
@Id
private Long id;
private String name;
// Getters and setters
}
Here, the User entity will be mapped to the users table in the database.
@Id
The @Id annotation indicates the primary key of an entity. The field annotated with @Id will be used
to uniquely identify each record in the corresponding table.
1
2
3
4
5
6
7
8
9
10
11
import jakarta.persistence.Entity;
import jakarta.persistence.Id;
@Entity
public class User {
@Id
private Long id;
private String name;
// Getters and setters
}
In this example, the id field is the primary key for the User entity.
@Column
The @Column annotation is used to map a specific field of the entity to a column in the database table.
You can use it to specify column attributes such as the column name, length, and whether it is nullable.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
import jakarta.persistence.Entity;
import jakarta.persistence.Id;
import jakarta.persistence.Column;
@Entity
public class User {
@Id
private Long id;
@Column(name = "user_name", nullable = false, length = 100)
private String name;
// Getters and setters
}
In this case, the name field is mapped to the user_name column in the database, with a maximum length of
100 characters and a non-null constraint.
Configuring Relationships with Annotations
Hibernate makes it easy to define relationships between entities using annotations. The most common relationships are one-to-one, one-to-many, many-to-one, and many-to-many.
@OneToOne
The @OneToOne annotation is used to define a one-to-one relationship between two entities.
In this type of relationship, each instance of one entity corresponds to exactly one instance of another entity.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
import jakarta.persistence.Entity;
import jakarta.persistence.Id;
import jakarta.persistence.OneToOne;
@Entity
public class User {
@Id
private Long id;
@OneToOne
private Address address;
// Getters and setters
}
In this example, each User has one Address, and each Address corresponds to one User.
@OneToMany
The @OneToMany annotation is used to define a one-to-many relationship, where one entity can be associated
with multiple instances of another entity.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
import jakarta.persistence.Entity;
import jakarta.persistence.Id;
import jakarta.persistence.OneToMany;
import java.util.List;
@Entity
public class User {
@Id
private Long id;
@OneToMany(mappedBy = "user")
private List<Order> orders;
// Getters and setters
}
Here, a User can have multiple Order entities associated with it.
@ManyToOne
The @ManyToOne annotation is used to define a many-to-one relationship, where many instances of one entity are
associated with a single instance of another entity.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
import jakarta.persistence.Entity;
import jakarta.persistence.Id;
import jakarta.persistence.ManyToOne;
@Entity
public class Order {
@Id
private Long id;
@ManyToOne
private User user;
// Getters and setters
}
In this example, many Order entities can be associated with a single User.
@ManyToMany
The @ManyToMany annotation is used to define a many-to-many relationship, where multiple instances of one entity are
associated with multiple instances of another entity.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
import jakarta.persistence.Entity;
import jakarta.persistence.Id;
import jakarta.persistence.ManyToMany;
import java.util.List;
@Entity
public class Student {
@Id
private Long id;
@ManyToMany
private List<Course> courses;
// Getters and setters
}
In this case, a Student can enroll in multiple Course entities, and each Course can have multiple Student
entities enrolled.
Working with @Embedded and @Embeddable for Composite Types
In Hibernate, you might encounter situations where a class represents a composite type, i.e.,
a type that doesn’t have its own identity but is part of an entity. This is where @Embedded
and @Embeddable annotations come into play.
@Embeddable
The @Embeddable annotation is used to mark a class as a component that can be embedded in other entities.
An Embeddable class does not have a separate table in the database; instead, its fields are stored in the table
of the entity that contains it.
1
2
3
4
5
6
7
8
9
import jakarta.persistence.Embeddable;
@Embeddable
public class Address {
private String street;
private String city;
// Getters and setters
}
In this example, the Address class is marked as @Embeddable, meaning it can be embedded in other entities.
@Embedded
The @Embedded annotation is used to embed an Embeddable class within an entity.
The fields of the Embeddable class will be mapped to columns in the entity’s table.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
import jakarta.persistence.Entity;
import jakarta.persistence.Id;
import jakarta.persistence.Embedded;
@Entity
public class User {
@Id
private Long id;
@Embedded
private Address address;
// Getters and setters
}
Here, the Address class is embedded within the User entity, meaning the street and city fields of Address
will be stored in the User table.
Conclusion
Hibernate annotations offer a powerful and convenient way to map Java objects to database tables
and define relationships between them. Understanding and effectively using annotations such as @Entity,
@Table, @Id, and @Column allows you to map your entities accurately. Additionally, configuring relationships
with @OneToOne, @OneToMany, @ManyToOne, and @ManyToMany is crucial for modeling the data relationships
in your application. Finally, @Embedded and @Embeddable annotations provide a clean way to work with
composite types, allowing for more complex and flexible data models. By mastering these annotations,
you can leverage Hibernate’s full potential to create robust and maintainable Java applications.