OneToOne and OneToMany annotations

OneToOne and OneToMany annotations

@OneToOne and @OneToMany :

In JPA (Java Persistence API), @OneToOne and @OneToMany are annotations used to define relationships between entities. These annotations specify how entities are related to each other in terms of cardinality and ownership.

OneToOne and OneToMany annotations

@OneToOne :

@OneToOne annotation is used to define a one-to-one relationship between two entities. It implies that each record in one entity is associated with exactly one record in another entity. The relationship can be bidirectional, where each entity holds a reference to the other.

@OneToMany :

@OneToMany annotation is used to define a one-to-many relationship between two entities. It indicates that one entity (the “one” side) can be associated with multiple instances of another entity (the “many” side). The relationship is typically unidirectional from the owning entity to the related entity.

Examples in Java

Let’s demonstrate how to use @OneToOne and @OneToMany annotations in JPA with Java examples.

1. @OneToOne Relationship Example
java
import javax.persistence.*;

@Entity
public class User {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    private String username;

    @OneToOne(mappedBy = "user", cascade = CascadeType.ALL)
    private UserProfile userProfile;

    // Getters and Setters
}

@Entity
public class UserProfile {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    private String fullName;

    @OneToOne
    @JoinColumn(name = "user_id")
    private User user;

    // Getters and Setters
}

  • Explanation : In this example, User and UserProfile have a bidirectional @OneToOne relationship:
    • User entity owns the relationship (mappedBy = “user”), meaning UserProfile entity has a reference to User.
    • UserProfile entity has a @OneToOne relationship with User, using @JoinColumn to specify the foreign key column (user_id) in the UserProfile table.

2. @OneToMany Relationship Example
java
import javax.persistence.*;
import java.util.List;

@Entity
public class Department {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    private String name;

    @OneToMany(mappedBy = "department", cascade = CascadeType.ALL)
    private List<Employee> employees;

    // Getters and Setters
}

@Entity
public class Employee {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    private String name;

    @ManyToOne
    @JoinColumn(name = "department_id")
    private Department department;

    // Getters and Setters
}

  • Explanation : In this example, Department and Employee have a unidirectional @OneToMany relationship:
    • Department entity owns the relationship (mappedBy = “department”), meaning Employee entities are associated with a single Department.
    • Employee entity has a @ManyToOne relationship with Department, using @JoinColumn to specify the foreign key column (department_id) in the Employee table.