spring-projects / spring-projects/spring-data-rest
Map<> Entity association stay empty when POST on parent entity [DATAREST-960]
@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
Benjamin Legendre opened DATAREST-960 and commented
Here a simple use case :
@Entity
@Table(name="orders")
public class Order {
@Id
@GeneratedValue
private Integer id;
private String name;
@ManyToMany
@JoinTable(name = "line_items",
joinColumns = { @JoinColumn(name="order_id") },
inverseJoinColumns = { @JoinColumn(name="product_id") })
@MapKeyColumn(name="line_number")
private Map<Integer, Product> lineItems;
@Entity
@Table(name="products")
public class Product {
@Id
@GeneratedValue
private Integer id;
private String name;
}
Doing POST http://localhost/orders :
{
"name" : "order-2",
"lineItems" : {
"1" : "http://localhost:15571/products/1",
"2" : "http://localhost:15571/products/2",
"3" : "http://localhost:15571/products/3"
}
}
returns:
HTTP/1.1 201
Location: http://localhost:15571/orders/2
Content-Type: application/hal+json;charset=UTF-8
Transfer-Encoding: chunked
Date: Tue, 13 Dec 2016 17:26:23 GMT
{
"name" : "order-2",
"_links" : {
"self" : {
"href" : "http://localhost:15571/orders/2"
},
"order" : {
"href" : "http://localhost:15571/orders/2"
},
"lineItems" : {
"href" : "http://localhost:15571/orders/2/lineItems"
}
}
}
}
Unfortunately the lineItems remains empty:
POST http://localhost:15571/orders/2/lineItems
HTTP/1.1 200
Content-Type: application/hal+json;charset=UTF-8
Transfer-Encoding: chunked
Date: Tue, 13 Dec 2016 17:29:52 GMT
{
"content" : { }
}
I don't know if it is a bug or something not implemented. Passing uris this way works well on others types of collections (Set<Entity>, List<Entity> etc.).
More generally i have not found any relevant way to alter the state of a Map association:
PATCH/PUT on the association resource results in what is described on DATAREST-308. In fact it is not possible to specify the "key" of Map using Content-Type: text/uri-list by design.
A small project is attached with Entities of the use case
Affects: 2.5.5 (Hopper SR5)
Attachments:
- sdr-map-bug-example.zip (7.19 kB)
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.