Home > AI > Backend > SpringBoot > mysql-connector-java >

@JoinColumn

@OneToOne

@Entity
class Office {
    // primary key
    @Id
    private Integer id;


    // foreign key
    @OneToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "addressId")
    private Address address;
}

will create a foreign key linking the Office entity with the primary key from the Address entity. The name of the foreign key column in the Office entity is specified by name property.

@OneToMany

@Entity
 class Employee {
    @Id
    private Long id;

    @OneToMany(fetch = FetchType.LAZY, mappedBy = "employee")
    private List<Email> emails;
}


@Entity
class Email {
    @Id
    private Long id;
    
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "employee_id")
    private Employee employee;
}

Leave a Reply