spring-projects / spring-projects/spring-data-rest
Unable to persist parent entity via child entity
Open
@odrotbohm is already working on this.
Since May 23, 2022.
status: waiting-for-triage
- Dominant language
- Java
- Stars
- 958
- Forks
- 568
- PR merge metrics
- No merged PRs in 30d
Description
Here is my sample code
https://github.com/cbot59/spring-data-many-to-one-examples
I want to be able to POST my parent entity through my child entity, but it seems it doesn't work. Is there any code I missed to be able to work as my expectation?
What I did
- POST
/api/productsvia Spring Data REST
curl -X POST --location "http://localhost:8080/api/products" \
-H "Accept: application/json" \
-H "Content-Type: application/json" \
-d "{
\"name\": \"Handphone\",
\"quantity\": 2,
\"brand\": {
\"name\": \"Samsung\",
\"createdDate\": \"2020-01-04\"
}
}"
- GET
/api/productsvia Spring Data REST, product is saved
{
"_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
}
}
- GET
/api/brandsvia Spring Data REST, brand is not saved
{
"_embedded": {
"brands": []
},
"_links": {
"self": {
"href": "http://localhost:8080/api/brands"
},
"profile": {
"href": "http://localhost:8080/api/profile/brands"
}
},
"page": {
"size": 20,
"totalElements": 0,
"totalPages": 0,
"number": 0
}
}
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Assessment
This issue has not been assessed yet.