spring-projects / spring-projects/spring-data-rest
SDR tries to update associated entity rather than the association for a PUT / PATCH [DATAREST-963]
@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
Brandon Vulaj opened DATAREST-963 and commented
In a case where a @ManyToOne relationship is set to exported=false, and the association is already created, SDR attempts to update the target entity rather than changing the association when attempting to modify the parent entity through a PUT or PATCH.
When I override the SDR PUT method with a custom Spring controller method, a simple save using the entity's repository works just fine. Example below.
I have a very simple class, with a @ManyToOne association.
@Entity
public class Place {
...
@ManyToOne
@RestResource(exported = false)
private Category category;
...
}
Both Place and Category have their own respective Repositories that are exported. But for our case, we need to have the Category field not exported here. IDs are exposed for Category.
However, when I try to update an existing Place with an existing Category, Hibernate fails the update.
Example PUT to /places/foo
{
...
"category": {
"name": "Farm",
"id": 4
},
...
}
`Caused by: org.hibernate.HibernateException: identifier of an instance of com.foo.bar.model.Category was altered from 2 to 4`
I'm not sure how that's even happening, given I don't set any Cascade options - Categories are managed elsewhere.
I don't want to have to write a custom Controller for Place, as even though I'm not exporting the field, this seems like a bug?
Affects: 2.5.5 (Hopper SR5)
Reference URL: http://stackoverflow.com/questions/41213382/updating-a-non-exported-manytoone-field-in-spring-data-rest-fails
1 votes, 2 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.