One-to-Many Unidirectional Mapping:

In a one-to-many unidirectional mapping, one entity has a collection of another entity, but the other entity does not have a reference back to the first entity. This means that you can navigate the relationship from the "one" side, but not from the "many" side.

Let's say we have two entities, Customer and Order, where one Customer can have many Order entities. Here's an example of how to map this relationship using Hibernate annotations:

      
  @Entity
  @Table(name = "customers")
  public class Customer {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    private String name;

    @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, orphanRemoval = true)
    @JoinColumn(name = "customer_id")
    private List<Order> orders = new ArrayList<>();

    // constructors, getters, and setters
  }
      
      
        
  @Entity
  @Table(name = "orders")
  public class Order {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    private String orderNumber;

    // constructors, getters, and setters
  }
        
      

@OneToMany: This annotation is used to define a one-to-many relationship between two entities. It is placed on the collection field of the entity that represents the "one" side of the relationship.

cascade = CascadeType.ALL: This attribute specifies that any changes made to the "one" side of the relationship should be cascaded to the "many" side.

fetch = FetchType.LAZY: This attribute specifies that the "many" side of the relationship should be fetched lazily.

orphanRemoval = true: This attribute specifies that any "many" side entities that are no longer referenced by the "one" side should be removed.

@JoinColumn: This annotation is used to specify the foreign key column in the "many" side table that references the "one" side table.

In this example, the Customer entity has a one-to-many relationship with the Order entity, where one Customer can have many Order entities. The @OneToMany annotation is used on the orders field in the Customer entity to specify the relationship. The @JoinColumn annotation is used to specify the foreign key column in the orders table that references the customers table.

One-to-Many Bidirectional Mapping:

In a one-to-many bidirectional mapping, one entity has a collection of another entity, and the other entity has a reference back to the first entity. This means that you can navigate the relationship from both the "one" side and the "many" side.

To illustrate this, let's modify the previous example to make it bidirectional. We'll add a Customer field to the Order entity:

        
  @Entity
  @Table(name = "customers")
  public class Customer {
      @Id
      @GeneratedValue(strategy = GenerationType.IDENTITY)
      private Long id;

      private String name;

      @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, orphanRemoval = true, mappedBy = "customer")
      private List<Order> orders = new ArrayList<>();

      // constructors, getters, and setters
  }
        
      
        
  @Entity
  @Table(name = "orders")
  public class Order {

        @Id
        @GeneratedValue(strategy = GenerationType.IDENTITY)
        private Long id;

        private String orderNumber;

        @ManyToOne(fetch = FetchType.LAZY)
        @JoinColumn(name = "customer_id")
        private Customer customer;

        // constructors, getters, and setters
  }
        
      

@ManyToOne: This annotation is used to define the many-to-one side of a bidirectional relationship. It is placed on the field that represents the "many" side of the relationship.

fetch = FetchType.LAZY: This attribute specifies that the "one" side of the relationship should be fetched lazily.

@JoinColumn: This annotation is used to specify the foreign key column in the "many" side table that references the "one" side table.

mappedBy: This attribute is used to specify the field in the "many" side entity that maps to the "one" side entity. It is placed on the collection field in the "one" side entity.

In this example, we've added a Customer field to the Order entity, and used the @ManyToOne annotation to specify the many-to-one relationship between Order and Customer. We've also used the mappedBy attribute of the @OneToMany annotation on the orders field in the Customer entity to specify that the relationship is bidirectional and that the customer field in the Order entity is the owning side of the relationship.

With these mappings in place, we can use Hibernate to perform CRUD operations on the Customer and Order entities and persist them to the database. Here's an example of how to use Hibernate to save a Customer and its associated `Order.