Skip to main content


Showing posts from 2019

The best way to map a @OneToOne relationship with JPA and Hibernate

One-To-One Shared Primary Key Relationship One to one relationship refers to the relationship between two entities/tables A and B in which one item/row of A may be linked with only one item/row of B, and vice versa. In this example, book and book_detail tables have a one-to-one relationship. A book has only one book detail, and a book detail belong to only one book. book_detail.book_id is a foreign key references to book_detail also uses its foreign key book_id as primary key so-called shared primary key.
@Id declares the entity identifier. @Column maps the entity's field with the table's column. If @Column is omitted, the field name of the entity will be used as column name by default. @OneToOne defines a one-to-one relationship between 2 entities. @JoinColumn defines foreign key column and indicates the owner of the relationship. mappedBy indicates the inverse of the relationship. unique = true enforces the unique constraint, 1 address belongs to only 1 library. @MapsId de…