Sobes.tech
Junior — Middle

What types of relationships between entities are implemented in Hibernate?

sobes.tech AI

Answer from AI

Hibernate implements the following types of relationships between entities:

  • One-to-One — one entity is related to one other entity.
  • One-to-Many — one entity is related to many other entities.
  • Many-to-One — many entities are related to one entity (inverse of One-to-Many).
  • Many-to-Many — many entities are related to many other entities.

Each of these relationships can be configured using annotations such as @OneToOne, @OneToMany, @ManyToOne, @ManyToMany, as well as specifying cascade operations and fetch strategies. For example:

@Entity
public class User {
    @OneToMany(mappedBy = "user", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
    private Set<Order> orders;
}

@Entity
public class Order {
    @ManyToOne
    @JoinColumn(name = "user_id")
    private User user;
}
What types of relationships between entities are… - sobes.tech