spring-projects / spring-projects/spring-data-rest

Unable to persist parent entity via child entity

Open
#2,138 1 comment 0 reactions 1 assignee View on GitHub

@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/products via 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/products via 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/brands via 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

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.