spring-data-rest icon indicating copy to clipboard operation
spring-data-rest copied to clipboard

Unable to persist parent entity via child entity

Open cbot59 opened this issue 2 years ago • 0 comments

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
  }
}

cbot59 avatar Apr 27 '22 00:04 cbot59