Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/shofiq0023/spring-data-jpa-demo
This is a simple demo project using Spring Data JPA for demonstrating data relations and features of JPA
https://github.com/shofiq0023/spring-data-jpa-demo
data-jpa database database-relationships java spring-boot
Last synced: about 19 hours ago
JSON representation
This is a simple demo project using Spring Data JPA for demonstrating data relations and features of JPA
- Host: GitHub
- URL: https://github.com/shofiq0023/spring-data-jpa-demo
- Owner: shofiq0023
- Created: 2023-09-21T17:22:24.000Z (about 1 year ago)
- Default Branch: master
- Last Pushed: 2023-10-31T15:53:34.000Z (about 1 year ago)
- Last Synced: 2024-03-20T22:20:52.145Z (8 months ago)
- Topics: data-jpa, database, database-relationships, java, spring-boot
- Language: Java
- Homepage:
- Size: 89.8 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Data relation with JPA mapping
This is a demonstration project for data mapping with Spring data JPA.### Some pointers of this project:
- There is no authentication in this project as this is a demo project.
- This project demonstrate the JPA Mapping like One-to-One, Many-to-One, Many-to-Many mapping.# One-to-One Mapping:
This mapping is where one entity with directly related with exactly one another entity. This is demonstrated by the **User** and **UserPersonalInfo** entity in the project.
In the **User** entity the **@OneToOne** annotation is used for the mapping. Cascade type is set to **ALL** for reflecting any changes made to the **User** entity.
```java
// Entity class
public class User {
// Other properties@OneToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "personal_info_id", referencedColumn = "id")
private UserPersonalInfo personalInfo;// Getters and setters
}
```
**@JoinColumn** annotation is used on the owning side of the relation. In My case **User** is the owning side. **@JoinColumn** is not necessary but for controll I included the annotation. This annotation defines the relation by using the other entity's "id".
On the other hand, for the **UserPersonalInfo** entity, I added the **@OneToOne** and **@JsonBackReference** annotation.
```java
// Entity class
public class UserPersonalInfo {
// Other properties@OneToOne(mappedBy = "personalInfo")
@JsonBackReference
private User user;// Getters and setters
}
```
Here the **@JsonBackReference** was used to avoid **Jackson data bind loop error**. This annotation is required if you want to return the entity class in response. I would suggest using a DTO for API response. I returned the entity class for the sake of this demonstration.# One-to-Many Mapping:
This mapping is where one entity is related to multiple other entities. This is the most commonly used relationship. This is demonstrated by the **Book** and **Author** entity in this project.
In this case the Book is the owning side of this relationship.
```java
// Entity class
public class Book {
@ManyToOne
@JoinColumn(name = "author_id")
@JsonBackReference
private Author author;
}// Entity class
public class Author {
@OneToMany(mappedBy = "author", cascade = CascadeType.ALL, orphanRemoval = true)
private List books;
}
```
Here the relationship is Unidirectional as we have specified the owning and reverse side of the relationship.