Sobes.tech
Junior — Middle

Can you list the types of associations used in Hibernate?

sobes.tech AI

Answer from AI

The main types of associations between entities used in Hibernate are:

  • 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.
  • Many-to-Many: many entities are related to many other entities.

Each of these associations can be configured using annotations such as @OneToOne, @OneToMany, @ManyToOne, @ManyToMany.

Example of One-to-Many:

@Entity
public class Department {
    @OneToMany(mappedBy = "department")
    private Set<Employee> employees;
}

@Entity
public class Employee {
    @ManyToOne
    @JoinColumn(name = "department_id")
    private Department department;
}