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

@OneToOne

Example

@Entity
 class Library {

    @Id
    @GeneratedValue
    private long id;

    @Column
    private String name;

    @OneToOne
    @JoinColumn(name = "address_id")
    private Address address;
}



@Entity
 class Address {

    @Id
    @GeneratedValue
    private long id;

    @Column(nullable = false)
    private String location;

    @OneToOne(mappedBy = "address")
    private Library library;
}


interface LibraryRepository extends CrudRepository<Library, Long> {}
interface AddressRepository extends CrudRepository<Address, Long> {}

Leave a Reply