Mapping Annotations in Hibernate
Introduction to Mapping Annotations
In Hibernate, mapping annotations are used to define the relationship between Java classes and database tables. These annotations provide a way to specify how the persistent data is handled by Hibernate. This tutorial will guide you through the fundamental mapping annotations available in Hibernate, how to use them, and provide examples to illustrate their usage.
1. @Entity Annotation
The @Entity
annotation is used to specify that a class is an entity and is mapped to a database table. Each instance of the entity class corresponds to a row in the table.
Example:
public class User {
// fields, getters, setters
}
2. @Table Annotation
The @Table
annotation is used in conjunction with the @Entity
annotation to specify the name of the database table to which the entity is mapped. If not provided, Hibernate uses the class name as the table name.
Example:
@Table(name = "users")
public class User {
// fields, getters, setters
}
3. @Id Annotation
The @Id
annotation is used to specify the primary key of the entity. Each entity must have a primary key to uniquely identify its instances.
Example:
public class User {
@Id
private Long id;
// other fields
}
4. @GeneratedValue Annotation
The @GeneratedValue
annotation is used to specify the strategy for primary key generation. It can take multiple strategies such as AUTO, IDENTITY, SEQUENCE, etc.
Example:
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
}
5. @Column Annotation
The @Column
annotation is used to specify the details of the column to which a field is mapped. It allows you to customize the column name, data type, length, and whether it can be null.
Example:
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(name = "username", nullable = false, length = 50)
private String username;
}
6. @OneToOne Annotation
The @OneToOne
annotation is used to define a one-to-one relationship between two entities. This means that for each record in one table, there is exactly one corresponding record in another table.
Example:
public class User {
@Id
private Long id;
@OneToOne
private Profile profile;
}
7. @OneToMany Annotation
The @OneToMany
annotation is used to define a one-to-many relationship between two entities. This means that one record in one table can be associated with multiple records in another table.
Example:
public class User {
@Id
private Long id;
@OneToMany(mappedBy = "user")
private List
}
8. @ManyToOne Annotation
The @ManyToOne
annotation is used to define a many-to-one relationship between two entities. This means that multiple records in one table can be associated with a single record in another table.
Example:
public class Order {
@Id
private Long id;
@ManyToOne
private User user;
}
Conclusion
Mapping annotations are a powerful feature of Hibernate that allows developers to define how Java classes map to database tables. By using these annotations effectively, you can create a robust data model with various relationships. This tutorial covered the fundamental mapping annotations, which are essential for any Hibernate-based project. Experiment with these annotations in your own projects to gain a deeper understanding of their functionality and capabilities.