Example code to demo how to persist parent entity through child entity
- Java 17
- Maven
Use Spring Boot Maven plugin to run the app.
mvn spring-boot:run
curl -X GET --location "http://localhost:8080/api/products" \
-H "Accept: application/json"
{
"_embedded": {
"products": [
{
"name": "Handphone",
"quantity": 2,
"_links": {
"self": {
"href": "http://localhost:8080/api/products/1"
},
"product": {
"href": "http://localhost:8080/api/products/1"
},
"brand": {
"href": "http://localhost:8080/api/products/1/brand"
}
}
}
]
},
"_links": {
"self": {
"href": "http://localhost:8080/api/products"
},
"profile": {
"href": "http://localhost:8080/api/profile/products"
}
},
"page": {
"size": 20,
"totalElements": 1,
"totalPages": 1,
"number": 0
}
}
curl -X POST --location "http://localhost:8080/products" \
-H "Accept: application/json" \
-H "Content-Type: application/json" \
-d "{
\"name\": \"Handphone\",
\"quantity\": 2,
\"brand\": {
\"name\": \"Samsung\",
\"createdDate\": \"2020-01-04\"
}
}"
{
"id": 1,
"name": "Handphone",
"quantity": 2,
"brand": {
"id": 1,
"name": "Samsung",
"createdDate": "2020-01-04"
}
}
curl -X GET --location "http://localhost:8080/api/brands" \
-H "Accept: application/json"
{
"_embedded": {
"brands": [
{
"name": "Samsung",
"createdDate": "2020-01-04",
"_links": {
"self": {
"href": "http://localhost:8080/api/brands/1"
},
"brand": {
"href": "http://localhost:8080/api/brands/1"
}
}
}
]
},
"_links": {
"self": {
"href": "http://localhost:8080/api/brands"
},
"profile": {
"href": "http://localhost:8080/api/profile/brands"
}
},
"page": {
"size": 20,
"totalElements": 1,
"totalPages": 1,
"number": 0
}
}
ID | NAME | QUANTITY | BRAND_ID |
---|---|---|---|
ID | CREATED_DATE | NAME |
---|---|---|
Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.
Please make sure to update tests as appropriate.