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

PUT creates new records for @OneToOne association [DATAREST-1428]

Open
#1,789 0 comments 0 reactions 1 assignee View on GitHub

@odrotbohm is already working on this.

Since Dec 31, 2020.

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

Description

Carlos Guzman opened DATAREST-1428 and commented

Let's say these are the entities and repository configurations:

@Entity
class Customer {
 @Id @GeneratedValue
 Long id;
 String name;
 @OneToOne(cascade = CascadeType.ALL)
 Info info;
}
@Entity
class Info {
 @Id @GeneratedValue
 Long id;
 String theInfo;
}
@RepositoryRestResource
interface CustomerRepository extends CrudRepository<Customer, Long> {
}

Nothing complex here. *Info* entity needs to be embedded in Costumer and persisted together. Only *Costumer* repository is exposed/created.

After creating a *Costumer* with POST, the records are stored as expected:

{
   "name": "John",
   "info": {
      "theInfo": "the info here"
   }
}

Customer table:

ID NAME INFO_ID
1 John 2

Info table:

ID THE_INFO
2 the info here

 

But when an update is made to *Costumer*, a new record is created for *Info* and the new ID assigned to *Costumer*:

{
   "name": "John Doe",
   "info": {
      "theInfo": "updated info"
   }
}

Customer table:

ID NAME INFO_ID
1 John Doe 3

Info table:

ID THE_INFO
2 the info here
3 updated info

Which is incorrect. A duplicated record is created in the *Info* table.
I'm expecting the same *Info* record and only the data being updated.
Something like this:

Customer table:

ID NAME INFO_ID
1 John Doe 2

Info table:

ID THE_INFO
2 updated info

The sample project is in https://github.com/cguZZman/put-issue-spring-data-rest.git

Also, I have the fix ready if the bug is confirmed


Affects: 3.1.10 (Lovelace SR10)

Reference URL: https://github.com/cguZZman/put-issue-spring-data-rest.git

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.