Sobes.tech
Junior — Middle

What types of relationships between entities are implemented in Hibernate?

sobes.tech AI

Answer from AI

The main types of relationships between entities implemented in Hibernate are:

  • One-to-One — one entity is related to one other entity. For example, a user and their profile.

  • One-to-Many — one entity is related to many other entities. For example, one order can contain many products.

  • Many-to-One — the inverse of One-to-Many, where many entities are related to one.

  • Many-to-Many — many entities are related to many other entities. For example, students and courses, where a student can attend many courses, and a course can have many students.

These relationships are implemented using annotations, for example:

@Entity
public class User {
    @OneToOne
    private Profile profile;

    @OneToMany(mappedBy = "user")
    private List<Order> orders;
}

@Entity
public class Order {
    @ManyToOne
    private User user;

    @ManyToMany
    private List<Product> products;
}
What types of relationships between entities are… - sobes.tech