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

Invalid PUT/POST behaviour for @OneToMany associations [DATAREST-771]

Open
#1,145 4 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

Mikhail Kadan opened DATAREST-771 and commented

Consider the following domain model:

@Entity
public class Content {

    @Id
    @GeneratedValue
    private long id;

    @ManyToOne
    private ContentGroup contentGroup;
}
@Entity
public class ContentGroup {

    @Id
    @GeneratedValue
    private long id;

    @OneToMany(cascade = CascadeType.ALL)
    private Set<Content> contentUni;

    @OneToMany(cascade = CascadeType.ALL, orphanRemoval = true)
    private Set<Content> contentUniOrphan;

    @OneToMany(mappedBy = "contentGroup", cascade = CascadeType.ALL)
    private Set<Content> contentBi;

    @OneToMany(mappedBy = "contentGroup", cascade = CascadeType.ALL, orphanRemoval = true)
    private Set<Content> contentBiOrphan;
}

First create some entities:

  • POST "Content-Type: application/json" /contentGroups "{}" -> /contentGroups/1
  • POST "Content-Type: application/json" /contents "{}" -> /contents/1
  • POST "Content-Type: application/json" /contents "{}" -> /contents/2
  • ...

Then perform the following operations:

  • PUT "Content-Type: text/uri-list" /contentGroups/1/contentUni "contents/1" -> OK, the content/1 is listed in /contentGroups/1/contentUni

  • POST "Content-Type: text/uri-list" /contentGroups/1/contentUni "contents/2" -> OK, the content/1 and the content/2 is listed in /contentGroups/1/contentUni

  • PUT "Content-Type: text/uri-list" /contentGroups/1/contentUniOrphan "contents/3" -> error, org.springframework.orm.jpa.JpaSystemException: A collection with cascade="all-delete-orphan" was no longer referenced by the owning entity instance: de.apollon.sample.domain.ContentGroup.contentBiOrphan; nested exception is org.hibernate.HibernateException: A collection with cascade="all-delete-orphan" was no longer referenced by the owning entity instance: de.apollon.sample.domain.ContentGroup.contentBiOrphan

  • POST "Content-Type: text/uri-list" /contentGroups/1/contentUniOrphan "contents/4" -> OK, the content/4 is listed in /contentGroups/1/contentUniOrphan

  • PUT "Content-Type: text/uri-list" /contentGroups/1/contentBi "contents/5" -> error, the content/5 is not listed in /contentGroups/1/contentBi

  • POST "Content-Type: text/uri-list" /contentGroups/1/contentBi "contents/6" -> error, the content/6 is not listed in /contentGroups/1/contentBi

  • PUT "Content-Type: text/uri-list" /contentGroups/1/contentBiOrphan "contents/7" -> error, org.springframework.orm.jpa.JpaSystemException: A collection with cascade="all-delete-orphan" was no longer referenced by the owning entity instance: de.apollon.sample.domain.ContentGroup.contentBiOrphan; nested exception is org.hibernate.HibernateException: A collection with cascade="all-delete-orphan" was no longer referenced by the owning entity instance: de.apollon.sample.domain.ContentGroup.contentBiOrphan

  • POST "Content-Type: text/uri-list" /contentGroups/1/contentBiOrphan "contents/8" -> error, the content/8 is not listed in /contentGroups/1/contentBiOrphan

Is it possible to make the last two operations work? I need both bidirectional mapped and orphan removed association, and my usecase is to change the order of the associated items (PUT, I will switch from Set to List for that) and add (POST)/remove (DELETE or PUT with overwrite) them from the association


Affects: 2.4.2 (Gosling SR2)

Attachments:

2 votes, 3 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.