https://github.com/ivangfr/spring-data-jpa-relationships
The goal of this project is to study the JPA relationships: one-to-one, one-to-many / many-to-one, and many-to-many.
https://github.com/ivangfr/spring-data-jpa-relationships
java jpa-relationships many-to-many many-to-one mapstruct one-to-many one-to-one postgresql spring-boot spring-data-jpa spring-web-mvc springdoc-openapi testcontainers
Last synced: 6 months ago
JSON representation
The goal of this project is to study the JPA relationships: one-to-one, one-to-many / many-to-one, and many-to-many.
- Host: GitHub
- URL: https://github.com/ivangfr/spring-data-jpa-relationships
- Owner: ivangfr
- Created: 2023-05-02T21:59:20.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2024-12-17T20:45:10.000Z (10 months ago)
- Last Synced: 2025-03-24T08:42:34.271Z (7 months ago)
- Topics: java, jpa-relationships, many-to-many, many-to-one, mapstruct, one-to-many, one-to-one, postgresql, spring-boot, spring-data-jpa, spring-web-mvc, springdoc-openapi, testcontainers
- Language: Java
- Homepage:
- Size: 313 KB
- Stars: 30
- Watchers: 2
- Forks: 16
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Funding: .github/FUNDING.yml
Awesome Lists containing this project
README
# spring-data-jpa-relationships
The goal of this project is to study the JPA relationships: `one-to-one`, `one-to-many` / `many-to-one`, and `many-to-many`.
## Proof-of-Concepts & Articles
On [ivangfr.github.io](https://ivangfr.github.io), I have compiled my Proof-of-Concepts (PoCs) and articles. You can easily search for the technology you are interested in by using the filter. Who knows, perhaps I have already implemented a PoC or written an article about what you are looking for.
## Additional Readings
- \[**Medium**\] [**Understanding Relationships in JPA: Introduction**](https://medium.com/@ivangfr/understanding-relationships-in-jpa-introduction-5416c8a7c8a9)
- \[**Medium**\] [**Understanding Relationships in JPA: One-to-One with Simple Primary Key**](https://medium.com/@ivangfr/understanding-relationships-in-jpa-one-to-one-with-simple-primary-key-7c32f7e13a6a)
- \[**Medium**\] [**Understanding Relationships in JPA: One-to-One with Shared Primary Key**](https://medium.com/@ivangfr/understanding-relationships-in-jpa-one-to-one-with-shared-primary-key-36596416fe56)
- \[**Medium**\] [**Understanding Relationships in JPA: One-to-Many with Simple Primary Key**](https://medium.com/@ivangfr/understanding-relationships-in-jpa-one-to-many-with-simple-primary-key-e2e975c67c31)
- \[**Medium**\] [**Understanding Relationships in JPA: One-to-Many with Composite Primary Key**](https://medium.com/@ivangfr/understanding-relationships-in-jpa-one-to-many-with-composite-primary-key-1d7724a2bf63)
- \[**Medium**\] [**Understanding Relationships in JPA: Many-to-Many with Simple Primary Key**](https://medium.com/@ivangfr/understanding-relationships-in-jpa-many-to-many-with-simple-primary-key-b38209e5c9b4)
- \[**Medium**\] [**Understanding Relationships in JPA: Many-to-Many with Simple Primary Key and Extra Column**](https://medium.com/@ivangfr/understanding-relationships-in-jpa-many-to-many-with-simple-primary-key-and-extra-column-817e8bdda465)
- \[**Medium**\] [**Understanding Relationships in JPA: Many-to-Many with Composite Primary Key and Extra Column**](https://medium.com/@ivangfr/understanding-relationships-in-jpa-many-to-many-with-composite-primary-key-and-extra-column-a939b107c7cd)
- \[**Medium**\] [**Mastering JPA Relationships: Practical Examples of Bidirectional Associations**](https://medium.com/@ivangfr/spring-data-jpa-6bb5cd745b46)## Prerequisites
- [`Java 21+`](https://www.oracle.com/java/technologies/downloads/#java21)
- Some containerization tool [`Docker`](https://www.docker.com), [`Podman`](https://podman.io), etc.## Start Environment
In a terminal and inside the `spring-data-jpa-relationships` root folder, run the following command:
```
docker compose up -d
```## Running application using Maven
In a terminal and inside the `spring-data-jpa-relationships` root folder, run the command below:
```
./mvnw clean spring-boot:run
```Once the application is running, you can access its Swagger website at http://localhost:8080/swagger-ui.html.
## Useful Commands
- **Postgres**
```
docker exec -it postgres psql -U postgres -d jparelationshipsdb
\d persons
select * from persons;
```
> Type `exit` to exit## Shutdown
- To stop the application, go to the terminal where it is running and press `Ctrl+C`;
- To stop and remove docker compose containers, network and volumes, go to a terminal and, inside the `spring-data-jpa-relationships` root folder, run the following command:
```
docker compose down -v
```## Running Tests
In a terminal and inside the `spring-data-jpa-relationships` root folder, run the following command:
```
./mvnw clean test
```## JPA relationships
### One-to-One with Simple Primary Key

\[**Medium**\]: [**Understanding Relationships in JPA: One-to-One with Simple Primary Key**](https://medium.com/@ivangfr/understanding-relationships-in-jpa-one-to-one-with-simple-primary-key-7c32f7e13a6a)
### One-to-One with Shared Primary Key

\[**Medium**\] [**Understanding Relationships in JPA: One-to-One with Shared Primary Key**](https://medium.com/@ivangfr/understanding-relationships-in-jpa-one-to-one-with-shared-primary-key-36596416fe56)
### One-to-Many with Simple Primary Key

\[**Medium**\] [**Understanding Relationships in JPA: One-to-Many with Simple Primary Key**](https://medium.com/@ivangfr/understanding-relationships-in-jpa-one-to-many-with-simple-primary-key-e2e975c67c31)
### One-to-Many with Composite Primary Key

\[**Medium**\] [**Understanding Relationships in JPA: One-to-Many with Composite Primary Key**](https://medium.com/@ivangfr/understanding-relationships-in-jpa-one-to-many-with-composite-primary-key-1d7724a2bf63)
### Many-to-Many with Simple Primary Key

\[**Medium**\] [**Understanding Relationships in JPA: Many-to-Many with Simple Primary Key**](https://medium.com/@ivangfr/understanding-relationships-in-jpa-many-to-many-with-simple-primary-key-b38209e5c9b4)
### Many-to-Many with Simple Primary Key and Extra Column

\[**Medium**\] [**Understanding Relationships in JPA: Many-to-Many with Simple Primary Key and Extra Column**](https://medium.com/@ivangfr/understanding-relationships-in-jpa-many-to-many-with-simple-primary-key-and-extra-column-817e8bdda465)
### Many-to-Many with Composite Primary Key and Extra Column

\[**Medium**\] [**Understanding Relationships in JPA: Many-to-Many with Composite Primary Key and Extra Column**](https://medium.com/@ivangfr/understanding-relationships-in-jpa-many-to-many-with-composite-primary-key-and-extra-column-a939b107c7cd)
## References
One-to-One
- https://vladmihalcea.com/the-best-way-to-map-a-onetoone-relationship-with-jpa-and-hibernate
- https://www.callicoder.com/hibernate-spring-boot-jpa-one-to-one-mapping-example/One-to-Many / Many-to-One
- https://vladmihalcea.com/the-best-way-to-map-a-onetomany-association-with-jpa-and-hibernate
- https://www.callicoder.com/hibernate-spring-boot-jpa-one-to-many-mapping-example/Many-to-Many
- https://vladmihalcea.com/the-best-way-to-use-the-manytomany-annotation-with-jpa-and-hibernate
- https://www.callicoder.com/hibernate-spring-boot-jpa-many-to-many-mapping-example/