How many types of association mapping are possible in hibernate?
In Hibernate, there are several types of association mappings that you can use to define relationships between entities. The main types of association mappings are:
One-to-One (1:1): This mapping defines a one-to-one relationship between two entities. For example, a user may have one profile.
One-to-Many (1:N): This mapping defines a one-to-many relationship where one entity is associated with multiple instances of another entity. For example, a department can have multiple employees.
Many-to-One (N:1): This is the inverse of the one-to-many relationship. It defines a many-to-one relationship where multiple instances of one entity are associated with a single instance of another entity. For example, many employees belong to one department.
Many-to-Many (N:N): This mapping defines a many-to-many relationship where multiple instances of one entity can be associated with multiple instances of another entity. For example, students can enroll in multiple courses, and each course can have multiple students.
In addition to these primary types, Hibernate also supports various configurations and options for these associations, such as:
Bidirectional and Unidirectional Associations: Associations can be bidirectional (both entities are aware of each other) or unidirectional (only one entity knows about the other).
Join Tables: For many-to-many relationships, a join table is often used to manage the associations.
Cascade Types: You can specify cascade operations (like PERSIST
, MERGE
, REMOVE
, etc.) to define how operations on one entity affect associated entities.
Fetch Types: You can define how associated entities are fetched (eagerly or lazily).
These mappings allow you to model complex relationships in your database schema effectively using Hibernate.