Association in Java Persistence with Spring Data and Hibernate
Overview
In Java Persistence API (JPA), an association refers to the relationship between two entity classes. These associations define how entities are related and how their lifecycles are managed. There are several types of associations in JPA, including:
- One-to-One Associations: Each instance of an entity is associated with exactly one instance of another entity.
- One-to-Many Associations: One entity is associated with multiple instances of another entity.
- Many-to-Many Associations: Multiple instances of one entity are associated with multiple instances of another entity.
These associations are crucial for defining the relational mapping between entities in a database.
One-to-One Associations
One-to-one associations can be mapped using different strategies:
- Shared Primary Key: Both entities share the same primary key.
- Foreign Key Join Column: A foreign key in one entity references the primary key of another entity.
- Join Table: A separate table is used to manage the association between the two entities.
These strategies allow for flexibility in how one-to-one relationships are implemented in a database schema.
One-to-Many Associations
A one-to-many association involves a collection, where one entity is associated with multiple instances of another entity. This is commonly used in scenarios where a single entity needs to reference multiple related entities. For example, a Department entity might have a one-to-many relationship with an Employee entity, where each department can have multiple employees.
Many-to-Many Associations
In a many-to-many association, multiple instances of one entity are associated with multiple instances of another entity. This type of association is often implemented using a join table that contains foreign keys referencing the primary keys of the associated entities.
Example
An example of a many-to-many association is the relationship between Category and Item. This association is depicted in Figure 9.13.
Figure 9.13: Many-to-many association between Category and Item.
Polymorphic Associations
Polymorphic associations in Hibernate allow a reference to an abstract class, such as BillingDetails, which can be any of its concrete subclasses at runtime. This is achieved without special configuration if the target class is mapped with @Entity and @Inheritance. This feature provides flexibility in handling different types of related entities through a common interface or base class.
FAQ (Frequently asked questions)
What are polymorphic associations in Hibernate?
How are polymorphic associations achieved in Hibernate?
What is an association in JPA?
What types of associations are explored in JPA?
What do associations define in JPA?
What does sharing a primary key in an association mean?
When is sharing a primary key often used?
Can you give an example of entities sharing a primary key?
How can one-to-one associations be mapped in JPA?
When are one-to-one associations used?
What is a one-to-many association?
When is a one-to-many association commonly used?
What types of associations can be defined in JPA?
What do associations define in a database?
What is a many-to-many association?
What example is given for a many-to-many association?