Collection Mapping related annotations

Annotations

Definition

@OneToMany

This annotation can be used to define the one-to-many relationship between the two entities.

Attributes:

  • mappedBy: it specifies the field in child entity that owns the relationship, and it indicates the inverse side of relationship.
  • cascade: Defines the cascade operation to the applied to associated entities. It allows the propagating the state transitions from parent entity to its associated child entities.
  • fetch: It configures the fetching strategy for the associated the entities. It specifies the associated entities should be the eagerly or lazily loaded from database.

@ManyToOne

This annotation is used to defines the many to one relationship between the two entities.

Attributes:

  • fetch: It configures the fetching strategy for associated entity.
  • optional: It indicates whether association is the optional or mandatory. If set to false, then the associated entity must always be present.
  • targetEntity: This specifies the target entity class if the attributes type is the superclass or the interface.

@ManyToMany

This annotation defines the many to many relationship between the two entities.

Attributes:

  • mappedBy: It specifies the field in child entity that owns the relationship, and it indicates the inverse side of relationship.
  • cascade: Defines the cascade operation to the applied to associated entities. it allows the propagating the state transitions from parent entity to its associated child entities.
  • targetEntity: This specifies the target entity class if the attributes type is the superclass or the interface.

@ElementCollection

This annotation can be used to define the collection of the basic or the embeddable tables.

Attributes:

  • targetClass: This specifies the class of the elements in the collection.
  • fetech: It configures the fetching strategy for the elements in collection.
  • cascade: Defines the cascade operations to applied to elements in the collections.

@JoinTable

This annotation specifies the mapping of the many to many relationships using the join table.

Attributes:

  • name: It specifies the name of join table.
  • joinColumns: Defines the foreign key columns of the join table that can reference the inverse entity.
  • inverseJoinColumns: Defines the foreign key columns of the join table that can reference the inverse entity.

@OrderBy

This annotation specifies the ordering of the elements in the collection.

Attributes:

  • value: It specifies the property by the which the elements should be ordered.
  • asc: It indicates whether the ordering should be ascending order or descending order.

JPA – Collection Mapping

In Java, JPA can defined as Java Persistence API. Collection Mapping is a powerful feature that allows developers to map relationships between the entities and collections in the database. It allows the developers to map the collections of the objects from Java to corresponding database structures.

Similar Reads

Collection Mapping

Collection Mapping in JPA refers to the mapping scheme between a Java Collection and other database tables such as Set, List, and Map. It enables developers to establish relationships between companies and collections, enabling efficient data storage and retrieval....

Steps to Implement Collection Mapping

Below are the steps to implement JPA Collection Mapping....

Collection Mapping related annotations

Annotations Definition @OneToMany This annotation can be used to define the one-to-many relationship between the two entities. Attributes: mappedBy: it specifies the field in child entity that owns the relationship, and it indicates the inverse side of relationship.cascade: Defines the cascade operation to the applied to associated entities. It allows the propagating the state transitions from parent entity to its associated child entities.fetch: It configures the fetching strategy for the associated the entities. It specifies the associated entities should be the eagerly or lazily loaded from database.@ManyToOne This annotation is used to defines the many to one relationship between the two entities. Attributes: fetch: It configures the fetching strategy for associated entity.optional: It indicates whether association is the optional or mandatory. If set to false, then the associated entity must always be present.targetEntity: This specifies the target entity class if the attributes type is the superclass or the interface.@ManyToMany This annotation defines the many to many relationship between the two entities. Attributes: mappedBy: It specifies the field in child entity that owns the relationship, and it indicates the inverse side of relationship.cascade: Defines the cascade operation to the applied to associated entities. it allows the propagating the state transitions from parent entity to its associated child entities.targetEntity: This specifies the target entity class if the attributes type is the superclass or the interface.@ElementCollection This annotation can be used to define the collection of the basic or the embeddable tables. Attributes: targetClass: This specifies the class of the elements in the collection.fetech: It configures the fetching strategy for the elements in collection.cascade: Defines the cascade operations to applied to elements in the collections.@JoinTable This annotation specifies the mapping of the many to many relationships using the join table. Attributes: name: It specifies the name of join table.joinColumns: Defines the foreign key columns of the join table that can reference the inverse entity.inverseJoinColumns: Defines the foreign key columns of the join table that can reference the inverse entity.@OrderBy This annotation specifies the ordering of the elements in the collection. Attributes: value: It specifies the property by the which the elements should be ordered.asc: It indicates whether the ordering should be ascending order or descending order....

Project Implementation of JPA Collection Mapping

Step 1: Create the new JPA project using the Intellj Idea named jpa-collection-mapping-demo....