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

Partial Update with association flushes changes too early - eclipselink [DATAREST-722]

Open
#1,093 1 comment 0 reactions 1 assignee View on GitHub

@odrotbohm is already working on this.

Since Dec 31, 2020.

type: bug
Dominant language
Java
Stars
958
Forks
568
PR merge metrics
No merged PRs in 30d

Description

Mathias D opened DATAREST-722 and commented

I am having a problem with partial updates in spring data rest using eclipselink as the JPA-provider.

From the description the issue seems to be related to DATAREST-441 - but I decided to create a separate issue for it because I am not sure if it is really the same issue.

Please find a reproduction project here:
https://github.com/mduesterhoeft/spring-data-rest-partial-update-issue/tree/jpa_with_eclipselink

I am having a simple entity defined like this:


public class Person {

    @GeneratedValue
    @Id
    private Long id;

    private String name;

    @ManyToOne
    @JoinColumn(name = "CHILD_ID")
    private Child child;
}

In a RepositoryEventHandler I try to determine if the name changed and execute some logic if it changed - PersonRepositoryEventHandler

@RepositoryEventHandler
@Component
public class PersonRepositoryEventHandler {

...

    @HandleBeforeSave
    public void compareName(Person newPerson) {
        entityManager.detach(newPerson);

        Person oldPerson = personRepository.findOne(newPerson.getId());

        if (oldPerson.getName().equals(newPerson.getName())) {
            //some logic here
        }
    }
}

If I send in the following payload the old and new name in the event handler actually differ. Also from the logging output I can see that the BeforeSave handler method is invoked first and the update of the name field follows after that - so this works as expected.

{"name":"newName"}

When I update the association to the child in the same request I see a different behaviour:

  1. update statement updating the name field is issued (this is already happening before RepositoryEntityController#patchItemResource is invoked - so it happens during JSON deserialization)
  2. the BeforeSave handler method is invoked (here old and new name carry the same value already and I cannot determine if it has been changed)
  3. the update statement to update the child association is issued.
{
  "name":"newName",
   "child":"http://localhost/children/7"
}

My example project has a test for both cases - see com.example.PersonResourceTest

The repository also contains the example project using hibernate - as I said this works fine - https://github.com/mduesterhoeft/spring-data-rest-partial-update-issue/tree/jpa_with_hibernate

With the current situation I cannot reject certain updates in BeforeSave handlers and validators


Affects: 2.4.1 (Gosling SR1)

Reference URL: https://github.com/mduesterhoeft/spring-data-rest-partial-update-issue/tree/jpa_with_eclipselink

3 votes, 4 watchers

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.