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

Issue with One-to-Many and @HandleBeforeSave method [DATAREST-648]

Open
#1,027 5 comments 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

Andrei Smolkin opened DATAREST-648 and commented

We have One-to-Many relation (entity1 -> entity2) and a @HandleBeforeSave method where we update another entity using the JPA repository, the event is initiated by the HTTP PUT call. When we do so ( entity3Repository.save(entity3)) Hibernate flash event is happening and we are getting the following exception: "javax.persistence.EntityExistsException: A different object with the same identifier value was already associated with the session" related to the entity2. There is no any relation between entity3 and entity1, entity2.

Everything is working fine with 'org.springframework.data:spring-data-rest-webmvc:2.2.1.RELEASE' and we start experiencing the issue described above with any of the future releases including the very recent snapshot 'org.springframework.data:spring-data-rest-webmvc:2.4.0.BUILD-SNAPSHOT'.

If as a workaround we make a call to entityManager.merge(entity1) inside the @HandleBeforeSave handler (before the repository save() call) then it solves the issue.


Affects: 2.2.3 (Evans SR3), 2.3.2 (Fowler SR2), 2.4 RC1 (Gosling)

Attachments:

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.