This tutorial will walk you through the steps of mapping a JPA and Hibernate One to One shared primary key bidirectional entity relationships example with Spring Boot, Spring Data JPA, Lombok, MySQL and Docker
What you will need
- JDK 8+ or OpenJDK 8+
- Maven 3+
- MySQL Server 5+ or Docker CE 18+
Init project structure and dependencies
├── src │ └── main │ ├── java │ │ └── com │ │ └── hellokoding │ │ └── jpa │ │ ├── book │ │ │ ├── Address.java │ │ │ ├── AddressRepository.java │ │ │ ├── Library.java │ │ │ └── LibraryRepository.java │ │ └── Application.java │ └── resources │ └── application.properties ├── Dockerfile ├── docker-compose.yml └── pom.xml
Define JPA Entities and Repositories
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_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.
Define JPA Entities
JPA Entity is defined with
@Entity annotation, represent a table in your database.
@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.
@MapsId defines embedded primary key,
book_detail.book_id is embedded from
Spring Data JPA Repository
Spring Data JPA contains some built-in
Repository abstracting common functions based on
EntityManager to work with database such as
deleteById. All we need for this example is
Define Properties and Creating data
hk-mysql refers to Docker Compose service defined in the below
spring.jpa.hibernate.ddl-auto=create allows JPA/Hibernate auto create database and table schema for you.
On production environment, you may like to disable the DDL Auto feature by using spring.jpa.hibernate.ddl-auto=validate or spring.jpa.hibernate.ddl-auto=none (default). Check out this example as one of the approaches Database Migration/Evolution Example with Flyway and JPA/Hibernate
Creating data with JPA and Hibernate
CascadeType.ALL, associated entity
BookDetail will be saved at the same time with
Book without the need of calling its save function explicitly
Run the example
Run with Docker
Prepare Dockerfile for Java/Spring Boot application and docker-compose.yml for MySQL Server
Type the below command at the project root directory, make sure your local Docker is running
Access to MySQL Server docker container by issuing below bash command and key in
docker exec -it hk-mysql mysql -p
Query schema and data created by JPA/Hibernate based on your mapping
Run with JDK/OpenJDK, Maven and MySQL Server local
localhost and type the below command at the project root directory
mvn clean spring-boot:run