spring-projects / spring-projects/spring-data-rest
Invalid PUT/POST behaviour for @OneToMany associations [DATAREST-771]
@odrotbohm is already working on this.
Since Dec 31, 2020.
- 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/1POST "Content-Type: application/json" /contents "{}"->/contents/1POST "Content-Type: application/json" /contents "{}"->/contents/2- ...
Then perform the following operations:
-
PUT "Content-Type: text/uri-list" /contentGroups/1/contentUni "contents/1"-> OK, thecontent/1is listed in/contentGroups/1/contentUni -
POST "Content-Type: text/uri-list" /contentGroups/1/contentUni "contents/2"-> OK, thecontent/1and thecontent/2is 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, thecontent/4is listed in/contentGroups/1/contentUniOrphan -
PUT "Content-Type: text/uri-list" /contentGroups/1/contentBi "contents/5"-> error, thecontent/5is not listed in/contentGroups/1/contentBi -
POST "Content-Type: text/uri-list" /contentGroups/1/contentBi "contents/6"-> error, thecontent/6is 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, thecontent/8is 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:
- spring-data-rest-1-n-association-put.zip (124.66 kB)
2 votes, 3 watchers
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Assessment
This issue has not been assessed yet.