2

I have the model Person with relations to Account and School models:

@Entity
public class Person {
     @GeneratedValue
     @Id
     private Long id;

     @OneToOne(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
     @RestResource(exported = false)
     private Account account;

     @ManyToOne(fetch = FetchType.EAGER)
     private School school;
}

The idea - is that account should be editable only with user model. For the school in my requests I'm sending the url something like that:

"school": "http://localhost:8080/schools/2"

It's working ok for POST requests. I'm sending json like that:

{
    "account": {
        "username": "test",
        "email": "testEmail"
    },
    "school": "http://localhost:8080/schools/2"
}

After that request new person and account are created, for school_id - I have needed value in database.

It's working ok for PATCH requests. I'm sending json like that:

{
    "account": {
        "username": "new test",
        "email": "new testEmail"
    },
    "school": "http://localhost:8080/schools/1"
}

After that request needed fields are updated in database.

The issue in PUT request. I'm sending json like that:

{
    "account": {
        "username": "put test",
        "email": "put testEmail"
    },
    "school": "http://localhost:8080/schools/2"
}

The result of this request:

  • in accounts table I have new row, old account row is not updated and not deleted;

  • school is not updated.

I want to have the same behavior for PUT operations as I have for PATCH request. I need to update needed fields by PUT request. I need a help. What should I change?

TestName
  • 378
  • 2
  • 13
  • 1
    Please provide the code of the Web service method which processes the PUT request. Without it, it's ipossible to say why the entities are not updated as expected. – Antot Mar 25 '18 at 09:23
  • 1
    [Spring Data REST - PUT request does not work properly since v.2.5.7](https://stackoverflow.com/questions/45620195/spring-data-rest-put-request-does-not-work-properly-since-v-2-5-7) – Cepr0 Mar 25 '18 at 09:52
  • @Cepr0, thanks and what can we do as the workaround? always use PATCH instead of PUT? – TestName Mar 25 '18 at 11:48
  • @Трубецкой Yes. I use PATCH to update entity, and PUT for actions: https://speakerdeck.com/olivergierke/hypermedia-apis-with-spring-5?slide=20 – Cepr0 Mar 25 '18 at 14:04
  • @Cepr0, got it, thanks – TestName Mar 26 '18 at 08:56

0 Answers0